Nitin Aggarwal has Published 156 Articles

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

Nitin Aggarwal

Nitin Aggarwal

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

515 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 − ... Read More

Swift: declare an empty dictionary

Nitin Aggarwal

Nitin Aggarwal

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

74 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 ... Read More

Swift: Convert enum value to String?

Nitin Aggarwal

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. ... Read More

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

Nitin Aggarwal

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 ... Read More

Swift Array Check if an index exists

Nitin Aggarwal

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 ... Read More

Return multiple values from a function in Swift

Nitin Aggarwal

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 ... Read More

Less than or greater than in the Swift switch statement

Nitin Aggarwal

Nitin Aggarwal

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

1K+ 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, ... Read More

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

Nitin Aggarwal

Nitin Aggarwal

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

37 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 ... Read More

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

Nitin Aggarwal

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 ... Read More

Flatten an Array of Arrays in Swift

Nitin Aggarwal

Nitin Aggarwal

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

780 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 ... Read More

Advertisements