In order to split a given string into an array containing substrings from the original string, the components(separatedBy:) method can be used. Given a string of items separated by spaces, here is how to get an array of components separated by space: let str = “Foundation UIKit SwiftUI String Components” let items = str.components(separatedBy: ” …
How to use UserDefaults in SwiftUI
This tutorial will teach you how to use UserDefaults in SwiftUI. We are going to create a settings form view with several data entry controls such as text field, toggle, and a picker. Each of the controls will read and write its values from and to the UserDefaults database. If you’d like to learn more about …
SwiftUI Form tutorial – how to create and use Form in SwiftUI
In this tutorial we’re going to learn how to create and use forms in SwiftUI by building a simple settings form UI. Form in SwiftUI is a container which allows you to group data entry controls such as text fields, toggles, steppers, pickers and others. We’re going to explore and add each kind of data …
How to save enum in UserDefaults using Swift
UserDefaults allow you to store small pieces of data of the following types: Data, String, Number, Date, Array or Dictionary. Enumerations are not supported by UserDefaults so it’s not possible to store them directly. We are going to leverage raw values of enums to save them in UserDefaults because a raw value can be a string, a …
How to use UserDefaults in Swift
UserDefaults are meant to be used to store small pieces of data which persist across app launches. It is very common to use UserDefaults to store app settings or user preferences. UserDefaults lets you store key-value pairs, where a key is always a String and value can be one of the following data types: Data, …
Get a character from string using its index in Swift
Very often I find myself trying to access a character in string using a given index, however due to the way strings are implemented in Swift, it’s not that easy. Ideally, given a string, I’d like to access a character at index 3 like this: let input = “Swift Tutorials” let char = input[3] Unfortunately, …
SwiftUI stepper tutorial – how to create and use stepper in SwiftUI
Stepper is a user interface control which enables you to increment or decrement a value by tapping on its plus or minus elements. Stepper in SwiftUI is very similar to UIStepper in UIKit. This tutorial will teach you how to create and use a stepper in SwiftUI. In order to read a value from a …
SwiftUI slider tutorial – how to create and use slider in SwiftUI
Slider is a user interface control which enables you to select a value from a specified linear range of values by using touch. Slider in SwiftUI is very similar to a UISlider in UIKit. This tutorial will teach you how to create and use a slider in SwiftUI. In order to read a value from …
How to use tuples in Swift
Tuples in Swift are very lightweight data types which enable you to create, store and pass around groups of values which can be of different types. This can be very useful when trying to return multiple values from a function in Swift as a single compound value. Creating tuples How to define a basic tuple which …
How to create and use Picker with Form in SwiftUI
Picker is a control in SwiftUI which allows you to select a value from a list of possible options. In order to properly use a Picker, you need to back it with an array of possible options to choose from and a State variable storing the index of selected option in the array. In its …