Loading...
question swift November 17, 2023 How to loop through an array of structs in Swift?

Looping through an array of structs in Swift is straightforward and can be done in several ways depending on what you need to achieve. Here’s how to do it:

question swift November 17, 2023 How do get the string value of an enum in Swift?

In Swift, to get the string value of an enum, you typically have a couple of approaches depending on the enum’s definition. Let’s go through them:

question swift November 17, 2023 Different ways to compare strings in Swift

Comparing two strings in Swift is straightforward and can be done using the equality operator ==. This operator checks if two strings are exactly the same in terms of their characters and the order in which these characters appear.

question swift November 16, 2023 How to check whether a value is not nil in Swift and assign it to a variable at the same time?

In Swift, there are several ways to check for nil and assign a value to a variable, depending on the context and what you want to achieve. Here are some common approaches:

question swiftui swift October 30, 2023 How to convert Color type to hex and vice-versa while retaining alpha information?

The below should work both on macOS and iOS with one minor change. That is, use UIColor instead of NSColor if you’re planning to use it for iOS.

question swift macOS October 29, 2023 How to make Color conform to RawRepresentable in Swift in macOS?

For various reasons you may want to convert the Color type to a String. And, below is a relatively cleaner way to do it.

question swift October 22, 2023 How to not break automatically in switch statements in Swift?

In Swift, the switch statement doesn’t automatically fall through to the next case. Each case block is designed to execute only the code within that case, and it doesn’t continue to the next case unless you use the fallthrough keyword.

question swift xcode October 8, 2023 How to get rid of 'Result of call to function is unused' warning in Swift/Xcode?

In Swift, if you encounter a “Result of call to ‘function’ is unused” warning, it means that you’re calling a function that returns a value (typically a result type, such as Result or any other type), but you’re not doing anything with the result. To get rid of this warning, you have a few options depending on the specific situation:

question swiftui swift September 30, 2023 How to open the Settings view in a SwiftUI app on macOS 14.0 (Sonoma)?

In macOS 14.0 (Sonoma), Apple removed support for NSApp.sendAction to open the Settings view in your SwiftUI app. You now have to use SettingsLink like below:

question swiftui swift May 29, 2022 How to open a new window in SwiftUI?

Although many things in SwiftUI are idiomatic and straightforward, showing your view in a new window needs a bit of coding to do. Hence, this short post.

question macOS swift November 10, 2021 How to detect fn key press in Swift?

You can override the flagsChanged() method of NSViewController and have your code like below to detect fn key press and release in macOS:

question macOS swift March 14, 2021 How to ignore mouse events in a view or window in macOS?

You can ignore mouse events in a window/view by adding just a single line of code.

question macOS swift March 14, 2021 How to detect Escape key pressed in macOS apps?

Like Delete key, detection of Escape key press is also slightly different than detecting general key presses.

question swift March 14, 2021 How to generate a random number in Swift?

You can use the random method in Int struct for this.

question macOS swift August 14, 2020 How to quit or close an app in macOS using Swift?

You can quit or exit an app with:NSApp.terminate(self)

question macOS swift August 14, 2020 How to detect Delete key press in Swift?

Delete key press detection is slightly different than other keys. It uses NSDeleteCharacter like below:

question swift xcode August 12, 2020 How to remove a Swift package from a project in Xcode?

If you go to Xcode > File > Swift Packages, you can see options to add a new Swift package, update them, reset caches, and resolve package versions. However, you do not see an option to remove a particular Swift package.

question swift August 9, 2020 How to iterate an array in reverse in Swift?

From Swift 5.0, you can use any of the following approaches to iterate an array in reverse.

question macOS swift August 6, 2020 How to open an app's window on top of all others in Swift?

You can open your app’s window on top of all other open application windows with the below code:

question macOS swift August 6, 2020 How to keep an app's window always on top of others in Swift?

Before launching the window, just use the appropriate window level, and you’re done.

June 7, 2020 How to add Global Key Shortcuts to your macOS app using MASShortcut

Adding Global Keyboard Shortcuts to your macOS app can be a pain as there isn’t a Cocoa API for the same. You would have to rely on the old, most of which are deprecated, Carbon API.