How to use switch statement with enum in Swift

Swift language provides a switch statement which allows you to compare a given value against multiple possible matching options and perform respective action. In its simplest form, a switch statement looks and works like that: let frameworkName = “SwiftUI” switch frameworkName { case “UIKit”: print(“Using UIKit”) case “SwiftUI”: print(“Using SwiftUI”) default: print(“Framework undefined”) } Given … 

 

How to present new sheet view from navigationBarItems button in SwiftUI

In this tutorial we’re going to learn how use NavigationView’s navigationBarItems button to present a new sheet view modally. In order to accomplish this, we’re going to follow these steps: Create a NavigationView Add a button to the NavigationView Create a new sheet view which we wish to present Add action to the button to present … 

 

How to expand SwiftUI views to span across entire width or height of screen

Learn how to use frame() modifier to expand SwiftUI views to take full width or height of screen. When applied to your view, the frame() modifier positions it within another view (frame) with specified dimensions. By manipulating the maximum width and/or height of the new frame, we’ll be able to achieve the desired effect of … 

 

How to pick a random element from an array in Swift

Swift Standard Library provides a randomElement() method to return a random element from an array or nil if the array is empty. If you know that your array is not empty, then go ahead and force unwrap the result of randomElement() method: let array = [“Swift”, “SwiftUI”, “UIKit”, “Foundation”] let randomElement = array.randomElement()! print(randomElement) However, … 

 

How to add borders to SwiftUI views

Learn how to add borders to SwiftUI views including: Text view, Stack view, Image view, and Button. Create rectangular or rounded-corners borders using .border() or .overlay() modifiers respectively. Create solid line or dashed borders. Add default, rectangular border to a Text view using .border() modifier: Text(“SwiftUI Tutorials”) .font(.largeTitle) .padding() .border(Color.black) Add rectangular border to a … 

 

Advanced SwiftUI button styling and animation

Learn how to style SwiftUI buttons using ButtonStyle protocol and .buttonStyle() modifier by creating reusable button styles. Using ButtonStyle protocol allows you to create advanced button styles and introduce animations to the button style when it is being pressed. Let’s start with a simple button: Button(action: { print(“Button action”) }) { HStack { Image(systemName: “bookmark.fill”) Text(“Bookmark”) … 

 

How to create a Button in SwiftUI

Learn how to create and use Buttons in SwiftUI. Style the button with text and images, add border around it. Perform actions when button is pressed and update SwiftUI view accordingly. SwiftUI Button is similar to UIButton from UIKit when it comes to behavior however you define it in a different way by specifying an … 

 

How to use SF Symbols in SwiftUI

While this tutorial mostly focuses on using SF Symbols in SwiftUI, there is a section at the end which covers how to use SF Symbols in UIKit. What are SF Symbols? I think Apple’s documentation explains it best: SF Symbols provides a set of over 1,500 consistent, highly configurable symbols you can use in your … 

 

How to convert String to Double or Float in Swift

Converting a String to a Double or Float can be easily achieved by using the if let statement in Swift. Take a look at the following example: var string = “12.2416” if let floatValue = Float(string) { print(“Float value = \(floatValue)”) } else { print(“String does not contain Float”) } If the String contains a valid … 

 

Floating point precision in Swift String

If you’d like to interpolate a float or a double with a String in Swift, use the %f String format specifier with appropriate number in order to specify floating point precision. Given a Float or a Double value: var value = 4.53978 Create a string with value rounded to one decimal place which results in …