Found 517 Articles for Swift

Swift optional escaping closure parameter

Nitin Aggarwal
Updated on 04-May-2023 12:59:11

955 Views

In Swift, an optional escaping closure parameter is a closure that can be executed after the function it is passed to has returned. In this article, we will see how to create escaping closures as parameters with examples. To declare an optional escaping closure parameter, you add the @escaping keyword before the closure type to the function's parameter list. Syntax Here is the syntax. func doSomething(completion: @escaping () -> Void) { // Write code here } In the above code, completion is an optional escaping closure parameter that takes no parameters and returns Void. The @escaping ... Read More

Swift integer conversion to Hours/Minutes/Seconds

Nitin Aggarwal
Updated on 04-May-2023 12:56:36

2K+ Views

In Swift, there are many approaches to converting an integer to time components like hours, minutes, and seconds. Every approach depends on the requirement. For example, you can use arithmetic operators to convert. Another approach is using DateComponentsFormatter class to convert an integer into time components easily. In this article, you will see many examples of converting an integer to time components. Example 1 To convert an integer representing a duration in seconds to hours, minutes, and seconds, you can use the following code in Swift − import Foundation let durationInSeconds = 3661 let hours = durationInSeconds / 3600 let ... Read More

Swift do-try-catch syntax and implementation

Nitin Aggarwal
Updated on 04-May-2023 12:54:20

3K+ Views

In Swift, the do-try-catch statement is used to handle errors that can be thrown by a function or method. It provides a structured way to catch and handle errors in your code. In your codebase, you cannot handle all runtime errors when they come but using try-catch you can handle them without crashing your application. The do-try-catch Syntax The do block is used to wrap code that could throw an error. Inside the do block, you call the function or method that throws an error. The try keyword is used before calling any function or method that may throw an ... Read More

Is key-value observation (KVO) available in Swift?

Nitin Aggarwal
Updated on 04-May-2023 12:51:22

998 Views

In Swift, you can use KVO to observe changes to an object property by registering an observer for that property. When the property value changes, the observer is notified and can take appropriate actions. In this article, you will see an example of how to implement KVO in Swift. To use KVO in Swift, you need to do the following Mark the property that you want to observe with the @objc dynamic attribute. This attribute tells the Swift compiler to generate Objective-C-compatible code for the property. Register an observer for the property using the addObserver(_:forKeyPath:options:context:) method of the observed ... Read More

How to save local data in a Swift app?

Nitin Aggarwal
Updated on 04-May-2023 12:49:42

2K+ Views

In Swift, there are several ways to save local data in an app, depending on the type and size of data you want to save. You can use User Defaults, Property List Serialization, Core Data, etc. In this article, you will learn about User Defaults with some examples. User Defaults In iOS, macOS, and watchOS, a fundamental storage mechanism called UserDefaults enables an app to store relatively small amounts of data, including user preferences or settings. You assign a value to a particular key in a key-value pair system that is used. The value may then be obtained by using ... Read More

How to manually deprecate members in iOS Swift?

Nitin Aggarwal
Updated on 04-May-2023 12:48:01

1K+ Views

In iOS Swift, you can manually deprecate members (properties, methods, and other members) by using the @available attribute with the deprecated argument. @available The @available attribute in Swift is used to specify the availability of a particular piece of code. It can be used to mark a class, function, method, property, or enumeration as available or unavailable for a particular platform, version, or architecture. Here's an example syntax of the @available attribute @available(platform version, *) The platform argument specifies the platform on which the code is available (e.g. iOS, macOS, watchOS, tvOS). The version argument specifies the version of ... Read More

How to create an empty array in Swift?

Nitin Aggarwal
Updated on 04-May-2023 12:45:15

2K+ Views

In Swift, there are several ways to create an empty array. All approaches are very easy to create an array. Manytimes, creating an empty array is most common requirement in your apps. You can create empty array of any type. In this article, you will see different ways to construct an empty array. Syntax In Swift, you can create an empty array of a certain type using the following syntax − var arrayName = [Type]() Or you can use this alternate syntax − var arrayName: [Type] = [] Both syntaxes work similarly in Swift. For example, if you ... Read More

How to call tap gestures on UIView programmatically in Swift?

Nitin Aggarwal
Updated on 04-May-2023 12:44:07

2K+ Views

In Swift, you can use the UITapGestureRecognizer class to add a tap gesture on view programmatically. This class provides you with different properties and methods to enable a tap gesture. In this article, you will learn how to add a tap gesture with an example. UITapGestureRecognizer class UITapGestureRecognizer is a built-in class in the UIKit framework that recognizes a tap gesture on a view. A tap gesture is a quick touch with a single finger or multiple fingers on the screen. UITapGestureRecognizer recognizes taps of a certain number of fingers, taps a certain number of times, and a combination of ... Read More

Why do I need underscores in Swift?

Nitin Aggarwal
Updated on 04-May-2023 12:35:08

1K+ Views

In Swift, underscores have many different uses for a different purposes. Here are some examples. Ignore unnecessary loop variables or return values. Absence of identifiers for external parameters in function calls. Even if they were initially specified as constants, making variables changeable. Ignoring tuple components or using discard values when managing errors. To Ignore a Value To ignore a value that a function or method returns in Swift, use an underscore. You could compose something like this, for instance, if you only worry about an operation's success or failure. This is the most common case you use in ... Read More

What's the cleanest way of applying map() to a dictionary in Swift?

Nitin Aggarwal
Updated on 04-May-2023 12:32:50

585 Views

In Swift, we can use the map() method to the dictionary to apply a transformation to the values of the dictionary. This method returns a newly created object with the same keys as the original dictionary but with the values transformed by the mapping function. Example 1: Transforming Values with a Closure In the following example, we are performing the multiplication on each value in the dictionary using the mapValues() function. We pass a closure that takes an argument. In the resulting dictionary, you can see that each value has been doubled. import Foundation let inputDictionary = [1: 2, 3: ... Read More

Previous 1 ... 7 8 9 10 11 ... 52 Next
Advertisements