Tagged: Swift

There are 113 items tagged #Swift.
And there a lots of other tags to explore.

How to break multiple nested loops in Swift

Learn how to break out of the outer loop from the inner loop.

Different ways to map over Dictionary in Swift

Learn how to map function work with Swift dictionary.

How to convert Degrees to Radians in Swift

In this article, we will learn different ways to convert Degrees to Radians in iOS.

Better print for AnyKeyPath in Swift 5.8

Swift 5.8 improve the information that is printed out for a keypath.

How to convert Radians to Degrees in Swift

In this article, we will learn different ways to convert Radians to Degrees in iOS.

Default Value in Swift Dictionary

Learn how to set and get value from a dictionary with a default value.

How to Filter an Array in Swift

Swift Array has an instance method, filter(_:), that removes unwanted elements from the resulting array.

Allow implicit self for weak self captures, after self is unwrapped

Swift allows implicit self in many places. It removes visual noise and allows developers to focus on things that matter. In Swift 5.8, Swift expands the case, which enables the implicit self to be used.

Back deploy Swift Function

Does this mean we can finally enjoy the new SwiftUI feature while still supporting old versions of iOS? Let's find out.

#file behavior change in Swift 5.8

In Swift 6.0, the behavior of the #file will change. Prepare yourself for the change.

Remove all limitations on variables in result builders

Swift 5.8, we have more flexibility in declaring local variables inside a result builder.

Conditional compilation for Attributes in Swift 5.8

Let's learn about the new conditional directive, hasAttribute.

Stable Sort in Swift

Is Swift sorting stable or not?

Getting All cases of Enum with Swift CaseIterable

There will be a time when you want to get an array of all enum cases, such as when you want to present them in a list or picker. CaseIterable protocol is what you are looking for.

What's New in Swift 5.8

Learn about all the Swift 5.8 updates.

Swift fileprivate vs private

Learn the difference between two similar access levels in Swift, private and fileprivate.

How to Deprecate old API in Swift

In Swift, we have many options to mark a method deprecated. Let's explore all of them.

Swift In-Out (inout) Parameters

Learn what is an inout parameter and how to use it.

How to fix "No exact matches in call to initializer" error in Swift

This error can happen in many places. Based on the message, it might not be obvious what is causing it and how to solve it. Let's learn how to fix it.

Computed property vs Method: Which one to use

A read-only computed property can use interchangeability with a method with no arguments. The question is, which one to use?

Swift Ternary operator (?:)

The Ternary operator is quite similar to if-else statements, but it isn't the same. Let's learn what it is and how to use it.

Swift private access level change in Swift 4

In Swift 4, there is a change in the private access level that means to end the use of fileprivate. Let's learn what it is all about.

How to remove duplicate items from Array in Swift

Learn how to do that using the Swift Algorithms module and without it.

What is Swift Guard Case

The guard-case is a syntax to allow us to precondition check for one specific enum case.

How to Save and Read Array in UserDefaults in Swift

Learn how to store an array in UserDefaults.

Unwrap Swift optional value in Switch case

Learn how to test for unwrapped optional values in a switch statement.

How to join an Array of strings into a Single string in Swift

Learn how to turn ["John", "Alice", "Bob"] into "John, Alice, Bob".

How to check if an Element is in an Array in Swift

Learn two ways to check if an array contains a given element.

What is Swift Computed Property

There are two kinds of properties in Swift, Stored, and computed. Let's learn the difference.

How to make Swift Enum conforms to Identifiable protocol

Learn how to make Swift Enum become Identifiable.

What is the difference between #available and @available

#available and @available are features related to the availability of APIs. They are tools that mean to use together. Let's learn the difference and when to use them.

Swift mod operator (%)

There might be several reasons that cause this error. I will share the solution that works for me.

How to declare Multiline String in Swift

If you need a string that spans several lines, Swift has a multiline string literal that you can use.

What is Swift If Case

Learn how to match a single case of an enum with an if-case syntax.

How to make parts of Text view Bold in SwiftUI

Learn how to style some parts of SwiftUI Text view.

How to remove nil elements from an array in Swift

Learn how to filter out nil values from a Swift array.

How to check if String is Number in Swift

Learn different ways to check if a string is a number.

How to get index and value from for loop in Swift

Swift has many ways to retrieve an index of an array, but it might not be obvious. Let's learn how to do it.

Loop n times in Swift

There are many ways to loop in Swift. In this post, I will show you how I do when I want to loop for a specific number of times.

How to capitalize the first letter in Swift

Learn how to uppercase to only the first letter of a word and sentence.

How to save enum with associated value in UserDefaults using Swift

Let's learn how to read/write enum with associated value to UserDefaults.

How to save enum in UserDefaults using Swift

Enumeration is a type that I think is perfect for representing options for users, but we can't save it directly to UserDefaults. Let's learn how to read/write enum to UserDefaults.

Improve numbers readability with underscores

A tip to prevent error and improve readability when dealing with numbers.

New if let shorthand for optional binding

Swift 5.7 introduced a new syntax for optional binding. Let's see what it is and how it can improve our code.

How to handle API Changes with @available

The available attribute (@available) is another tool that helps you cope with API changes. Let's see how this is different from the availability condition (#available).

How to handle API Changes with #available

Every year Apple introduces new features to the system, and sometimes they have to deprecate some old APIs to make room for the new ones. Change is an inevitable thing in programming. Let's learn how to handle the changes.

Swift Type placeholder: What is it and when to use it

Type placeholders allow us to write a type placeholder (_) in a place where type is expected. A compiler will automatically infer the type of that placeholder. But what is the benefit of it? Let's find out.

Swift typealias: What is it and when to use it

A type alias declaration introduces a named alias of an existing type into your app. You can think of it as defining a nickname for an existing type. Let's learn the benefit and when to use them.

How to set UserDefaults value with Launch Arguments

Learn how to use a launch argument to override UserDefaults value and test your apps.

How to parse ISO 8601 date in Swift

Learn how to parse ISO 8601 date with ISO8601DateFormatter.

How to customize automatic synthesizing Codable for enums with associated values

Learn what we can customize when relying on automatic synthesizing Codable for enums with associated values.

Codable synthesis for enums with associated values in Swift 5.5

Swift 5.5 extends the support for the automatic synthesis to enums with associated values. Learn what can be expected from the synthesis code.

How to decode enums ignoring case in Swift Codable

Learn how to make a case insensitive enum.

What do @main, @UIApplicationMain, and @NSApplicationMain mean

When you start a new project, you would see either of these attributes in an AppDelegate file. Let's find out its functionality.

How to make a custom button style supports leading dot syntax in SwiftUI

In Swift 5.5, we can apply button style using leading dot syntax, .buttonStyle(.plain), instead of a full name of a concreate type, .buttonStyle(PlainButtonStyle()). Let's see how we can make our custom button style support this.

What is a KeyPath in Swift

By learning about the key path, you open up yourself to an opportunity to improve your existing API or even create a new one that you don't aware you can do it.

What does the ?? operator mean in Swift

?? is an operator that has one specific use case. Let's find out what it is.

7 ways to pass a closure as an argument in Swift

There are many ways we can satisfy a closure argument. Some of them do not even look like closure. Let's learn all of them in this article.

How to set custom CodingKey for the convertFromSnakeCase decoding strategy

The thing you should know before using the convertFromSnakeCase decoding strategy.

How to reference a method with the same name and parameters but a different return type in Swift

Trying to reference two methods with the same name and parameters will cause an ambiguous compile error. Learn how to resolve it.

Different ways to catch throwing errors from Swift do-catch

It is tempting to just catch all a throwing error in a single catch clause and call it a day, but not all errors are created equals, and you should treat it as such.

How to explicitly specialize a generic function in Swift

Learn a workaround to specify a type for your generic functions.

New Formatters in iOS 15: Why do we need another formatter

We have a new way to format data into a localized string in iOS 15. Why do we need another formatter? How does it differ from the old one? Let's find out.

How to read App Name, Version, and Build Number from Info.plist

Learn an easy way to read the information in Info.plist.

What is a Property Wrapper in Swift

As the name implies, a property wrapper is a new type that wraps a property to add additional logic. Let's see what it capable of and the benefit it provided.

Sort array of objects by multiple properties with Swift Tuple

Learn how tuple can help you in sorting.

Does Swift enum retain its associated value

An easy question that you might overlook. Learn this simple fact together in this article.

How to create custom operators and do operators overloading in Swift

Learn how to overload existing operators such as +, -, *, / or create a custom one (such as .^.).

Different ways to check if a string contains another string in Swift

Learn how to check if a string contains another string, numbers, uppercased/lowercased string, or special characters.

Different ways to compare string in Swift

String comparison is an essential operation for day to day job. Swift provides a few variations for this. We will visit them in this article.

How to convert a String to an Int in Swift

Learn to convert a string "123" to an integer 123.

Different ways to sort an array of strings in Swift

Learn a proper way to sort an array of strings in each circumstance.

How to specify fractional digits for formatted number string in Swift

Learn how to format a Float and Double string.

How to use DateFormatter in Swift

Learn how to use this expensive DateFormatter.

How to loop in Swift

Learn different ways of using for loop for each scenario.

How expensive is DateFormatter

If you are working on iOS for long enough, there is a chance that you might have known that DateFormatter is expensive, but what is costly about DateFormatter? Let's find out in this article.

How to get the first N elements of array in Swift

Learn a few ways to do it and things you should know when using them.

Getting the number of days between two dates in Swift

There are a few variations when dealing with counting days. You need to ask yourself some questions beforehand.

Understanding Date and DateComponents

Date and time might be among your list of the hardest things in programming (It is for me). Today, I'm going to talk about a basic concept of a Date and its companion DateComponents.

What is @escaping in Swift closures

Learn the meaning of @escaping so you know what to do when you see it or when you need to write one.

Reduce boilerplate code with an automatic synthesis of Equatable and Hashable conformance

Equatable and Hashable are two essential protocols in the Swift world. Let's learn an old Swift feature that you might forget.

Setting default values for NSUserDefaults

NSUserDefaults is a go-to database for saving users' preferences. Learn how to populate it with your default values.

3 lesser-known ways of using Swift enums

Three language features around Swift enumeration that you might not aware of.

Testing delegates and protocols in XCTest

Learn how to write unit tests for delegate/protocol methods.

How to declare Swift protocol for a specific class

Learn how to create protocols that constrain their conforming types to a given class.

Sleep sort: A sorting algorithm without compare

I can't tell it is a stupid or genius algorithm, but it sure got a beauty in it.

Different ways to check for String suffix in Swift

Learn how to get a suffix from a Swift string.

Decode an array with a corrupted element

When working with an unstable, legacy, or third party API, you might get a malformed object in an array. Learn how to decode a JSON array with corrupted data in Codable safely.

Different ways to check for String prefix in Swift

Learn how to get a prefix from a Swift string.

How to compare two app version strings in Swift

Learn how to check your app version strings are higher or lower.

How to split a string into an array of substrings in Swift

Learn different ways to split a string into an array of substrings.

How to make multi-line text in UIButton

The default appearance of UIButton is a single line text, but it also supports a multi-line text with some minor tweak.

History of Auto Layout constraints

Learn different ways to define Auto Layout constraints programmatically.

How to preserve a struct memberwise initializer when you have a custom initializer

A tip to declare a custom initializer without losing a memberwise initializer.

Memberwise Initializers for Structure Types

Struct is one of the basic building blocks in your app. Today I'm going to share some tips about memberwise Initializers.

SF Symbols: What is it, and how to use?

Introduction of SF Symbols, learn the basics, and how to use them.

Class-only Protocols: class or AnyObject

If you are still declaring class-only protocols by inheriting them from class, you might need to revise your knowledge.

How to read a Property List (plist) into the code

When I want to keep some information or configuration out of a code base and don't want something fancy, I usually save it as Property List file. Let's see how to do it.

Sign in with Apple Tutorial, Part 4: Web and Other Platforms

Part forth in a series Sign in with Apple. Use Sign in with Apple JS to let users set up accounts and sign in to your website and apps on other platforms.

Print unescaped string output in Swift

How to print object (po) in a debugger (lldb) without escape special characters.

Sign in with Apple Tutorial, Part 3: Backend – Token verification

Part 3 in a series Sign in with Apple. In this part, we will see how backend can use the token to sign up/sign in users.

What is @discardableResult

Learn about this Swift attribute and its purpose.

Sign in with Apple Tutorial, Part 2: Private Email Relay Service

Part 2 in a series Sign in with Apple. In this part, we will talk about the anonymous email address. How to make it work and its limitation.

Sign in with Apple Tutorial, Part 1: Apps

Part 1 in a series Sign in with Apple. In the first part, we will focus on the app part. What we need to do to add Sign in with Apple option in our app.

Swift Documentation

How to write documentation comments in your Swift code.

if let: How not to use it

Learn how you should write a code that shows your true intention.

SwiftUI's ViewModifier

Learn a crucial concept in SwiftUI, view modifier, and a guide of how to create your custom modifier.

How to use SwiftUI in UIKit

Using SwiftUI as UIView and UIViewController

How to use UIKit in SwiftUI

Using UIView and UIViewController in SwiftUI

MVC (Missing View Controller)

Where is UIViewController in SwiftUI?

Enum & custom type from primitive JSON type

Swift Codable

Codable in Swift 4.0

Can it replace JSON encode/decode lib out there?

Where is my getter/setter in Swift?

A missing Objective-C piece

See all tags.