Found 517 Articles for Swift

What's the best practice for naming Swift files that add extensions to existing objects?

Nitin Aggarwal
Updated on 04-May-2023 12:30:28

567 Views

There is no single "best" practice for naming Swift files that add extensions to existing objects, but here are some commonly used conventions − Object Prefix Followed by Functionality String+Utilities.swift − adds utility functions to the String class Array+Sorting.swift − adds sorting functions to the Array class UIColor+Extensions.swift − adds color-related functions to the UIColor class Functionality Prefix Followed by Object CustomView+Animation.swift − adds animation functionality to a custom view class called CustomView JSONEncoder+CustomEncoding.swift − adds custom encoding and decoding functionality to the JSONEncoder class UICollectionViewLayout+Extensions.swift − adds layout-related functions to the UICollectionViewLayout class Descriptive Naming ... Read More

Swift: declare an empty dictionary

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

79 Views

In Swift, there are some different syntaxes to declare an empty dictionary. It is important to remember that all syntaxes produce the same result. In this article, you will see examples of how to declare an empty dictionary and define a dictionary in Swift. What is a Swift Dictionary? A dictionary is a collection in Swift that lets you keep key-value pairs. Each key in the dictionary has a corresponding value, and each word in the dictionary needs to be distinct. Because dictionaries are unordered, the sequence in which key-value pairs are introduced does not matter. Example In this example, ... Read More

Swift: Convert enum value to String?

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

4K+ Views

In Swift, you can convert an enum value to a String through the rawValue property. This is if the enum has a raw value of type String. If the enum doesn't have a raw value, you can use the String(describing:) initializer to get a string representation of the enum value. Also, you can use the CustomStringConvertible protocol. Example 1 Convert an enum value to a string using the rawValue property.In this example, Fruit is an enum with a raw value of type String. The rawValue property is used to get a string representation of the myFruit enum value, which is ... Read More

Swift: Call can throw, but it is not marked with 'try' and the error is not handled

Nitin Aggarwal
Updated on 04-May-2023 12:23:05

1K+ Views

In Swift, when you call a function that throws an error, you must either mark the function call with the try keyword or handle the error using a do-catch block. If you see the error message "Call can throw, but it is not marked with 'try' and the error is not handled", it means that you have called a function that can throw an error, but you have not handled the error properly. How to Fix These Errors? Mark the function call with the try keyword. For example − do { let result = try someFunctionThatCanThrow() ... Read More

Swift Array Check if an index exists

Nitin Aggarwal
Updated on 04-May-2023 12:19:21

1K+ Views

In Swift, there are several ways to check whether an index exists in an array. You can use startIndex, endIndex, the indices property, and the count property. In this article, you will see some examples of checking an index. Example 1: Using FirstIndex & EndIndex You can check if a specific index exists in a Swift array by comparing the index against the startIndex and endIndex properties of the array. Here's an example. import Foundation let inputArray = [1, 2, 3, 4, 5] let targetIndex = 3 if targetIndex >= inputArray.startIndex && targetIndex < inputArray.endIndex { print("Index ... Read More

Return multiple values from a function in Swift

Nitin Aggarwal
Updated on 04-May-2023 12:17:47

2K+ Views

In Swift, you can return multiple values from a function using a tuple. In this article, you will see different examples of how to use a tuple to return multiple values from a function. Here are some more practical examples of functions that return multiple values using tuples in Swift − Example 1 - Parsing a URL into its Components import Foundation func parseURL(urlString: String) -> (scheme: String, host: String, path: String) { guard let url = URL(string: urlString) else { fatalError("Invalid URL") } return ... Read More

Less than or greater than in the Swift switch statement

Nitin Aggarwal
Updated on 04-May-2023 12:16:05

2K+ Views

In Swift, there are many comparison operators to perform calculations and check different conditions. Less than or greater than operators are used to checking conditional statements. In this article, let's see how to use them with switch and if statements. Less Than or Greater Than the Switch Statement In Swift, you can use the case ..< and case ... syntax to define ranges in a switch statement. The case ..< syntax is used to define a range that includes all values greater than or equal to the first value and less than the second value Step 1 − If ... Read More

How to get the name of the enumeration value in Swift?

Nitin Aggarwal
Updated on 04-May-2023 12:13:41

43 Views

In Swift, you can conform to the CustomStringConvertible protocol to provide a default name for each case in an enumeration. This protocol can be used to provide custom meaning as per the requirement. CustomStringConvertible CustomStringConvertible is a protocol in Swift that defines a single property, description, which returns a String representation of an instance of a conforming type. By conforming to CustomStringConvertible, you can customize how your types are represented as strings when they are printed, logged, or otherwise converted to a string. When you conform to CustomStringConvertible, you define how instances of your type are represented as strings by ... Read More

How should I remove all the leading spaces from a string in Swift?

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

1K+ Views

In Swift, there are several approaches to removing the leading spaces from a string. There are some methods available like trimmingCharacters, drop, etc. You can also use the NSRegularExpression class to trim all leading spaces from a string. Using the trimmingCharacters() Method The trimmingCharacters(in:) method is a built-in method in Swift's String class that allows you to remove leading and/or trailing characters from a string. The method takes a single argument, which is a CharacterSet object that defines the set of characters to remove. You can create a CharacterSet object using the static properties of the CharacterSet class, such as ... Read More

Flatten an Array of Arrays in Swift

Nitin Aggarwal
Updated on 04-May-2023 12:05:12

930 Views

In Swift, you can use higher-order functions to flatten an array of arrays. You can use a combination of joined(), reduce(), and flatMap() functions. Sometimes, you are required to merge multiple arrays into a single array. In Swift, you can easily do that using high-order functions. In this article, we will see some examples of different use cases. Example 1: Using the flatMap() Function To flatten an array of arrays in Swift, you can use the joined() method along with the flatMap() function. Here's an example. import Foundation let arrayOfArrays = [[1, 2], [3, 4], [5, 6, 7]] let flattenedArray ... Read More

Advertisements