Nitin Aggarwal has Published 156 Articles

How to test the equality of Swift enums with associated values?

Nitin Aggarwal

Nitin Aggarwal

Updated on 11-Apr-2023 10:41:59

2K+ Views

In Swift, you can use the Equatable protocol to compare enums with associated values. In this article, we will see how we can compare them using the Equatable protocol with an example. Enumeration with Associated Values In Swift, you can provide a value along with an enum case. This makes ... Read More

How can I encode a string to Base64 in Swift?

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 11:47:14

5K+ Views

In Swift, you can use the built-in Data type and its method base64EncodedString() to encode a string to Base64. In the iOS applications, you might need to encode a string to base64 mostly in networking requests and responses. Mostly, you will need to encode a string to base64 and vice ... Read More

How do I make an enum decodable in Swift?

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 11:31:34

946 Views

In Swift, you can use the Codable protocol to make an enum decodable as well. In Swift, the Codable protocol is a very powerful concept to provide flexibility to decode and encode any type of value. Also, you can make an enum decodable as well. You have to just define ... Read More

How to convert a JSON string to a dictionary in Swift?

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 11:30:01

5K+ Views

Swift provides a class called JSONSerialization to convert a JSON string to dictionary format. For example, you are receiving a JSON string from the database, in order to use it in the application, you might need to convert it into a real object type. In this article, you will see ... Read More

How to convert a number to an absolute value in Swift?

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 11:26:50

2K+ Views

To convert a number to its absolute value in Swift, you can use the abs(_:) function. In this article, you will see multiple examples of how to use the abs() function in the Swift language. Example 1 In this example, you will convert a simple negative number to an absolute ... Read More

Convert a dictionary to JSON in Swift

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 10:53:14

7K+ Views

Swift provides a class called JSONSerialization to convert a dictionary to a JSON string. We will use the two steps to convert a dictionary to a JSON string in the Swift language. Here are the steps − Convert a dictionary into JSON data format using JSONSerialization.data() method which takes ... Read More

Array extension to remove an object by value in Swift

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 10:49:30

511 Views

To remove an object by value from an array in Swift, you can create an extension to the Array type. You can then call the function that takes the value to eliminate as a parameter. In this article, we will use the filter function to remove an object passing by ... Read More

How to convert float to int in Swift?

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 10:45:51

1K+ Views

In Swift, you can convert a float to an Int by using the Int() constructor. This will round the float value toward zero and return an integer value. Remember that, this constructor returns an optional value, so you have to use an optional binding approach to wrap the value safely. ... Read More

How to copy text to the clipboard/pasteboard with Swift?

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 10:44:36

2K+ Views

In Swift, there is a dedicated class called UIPasteboard that allows you to copy text to the pasteboard. The same class allows you to paste the text. A UIPasteboard class is part of the UIKit framework that provides a general processor for copying and pasting information in iOS applications. In ... Read More

How to iterate for loop in reverse order in Swift?

Nitin Aggarwal

Nitin Aggarwal

Updated on 04-Apr-2023 10:41:25

3K+ Views

In Swift, there are different approaches to iterating a for loop in reverse order. In this article, you will see some methods like reverse, ranges, stride(from:to:by:), forEach(), etc. Each method can be used in different use cases. Also, you should know that each method has its own performance. Using the ... Read More

Previous 1 ... 5 6 7 8 9 ... 16 Next
Advertisements