Found 517 Articles for Swift

How do I convert a Swift array to a string?

Nitin Aggarwal
Updated on 03-Jan-2023 10:18:55

1K+ Views

Let's look at some examples of how to convert an array to a string. Method 1:Using Joined(seperator:) Syntax Swift provides us with a method joined(separator:) of an array that can be used to convert a Swift array to a string. This method returns a new string by concatenating the elements of the array, separated by the provided separator string. let wordInString = words.joined(separator: ", ") In order to use the joined() method, call it by array along with passing the separator whatever you want. Algorithm Step 1 − Initialize your array Step 2 − Call joined() method with ... Read More

How do I concatenate or Merge Arrays in Swift?

Nitin Aggarwal
Updated on 03-Jan-2023 10:15:57

5K+ Views

Swift provides us with two different ways to concatenate or merge arrays in the Swift language.  You can use the + (plus) operator or the append() method. You will see other methods also of how you can merge multiple arrays in Swift. These methods are − Using plus (+) operator Using append(contentOf:) method Using flatMap() high-order function Using joined() method Using reduce() high-order function Method 1: Merge Arrays Using The + Operator Syntax let outputArray = firstArray + secondArray In order to use + operator to merge the arrays, simply it works as a binary operator that ... Read More

Does Swift have a Trim Method for Strings?

Nitin Aggarwal
Updated on 03-Jan-2023 10:09:55

2K+ Views

In Swift, you can use the trimmingCharacters(in:) method of the String type to trim parts of a string. It is an instance method that returns a newly created string made by removing the applied character set. This method accepts a parameter of type CharacterSet to perform leading and trailing characters. You can apply different types of CharacterSet to trim a string. There are some of the most commonly used CharacterSets, like − whitespaces whitespacesAndNewlines decimalDigits letters lowercaseLetters uppercaseLetters punctuationCharacters capitalizedLetters newlines You will learn some of them about how to perform different types of trimming of a string. ... Read More

Add an element to an array in Swift

Nitin Aggarwal
Updated on 03-Jan-2023 10:07:42

5K+ Views

In this article, you will learn how to add an element to an array in Swift. Before you begin to learn element insertion into an array, you will need to understand the type of array. Array Syntax // Declare an immutable array let arrayName: [valuetype] = [array element 1, array element 1, ...array element n] // Declare a mutable array var arrayName: [valuetype] = [array element 1, array element 1, ...array element n] In Swift, we can declare an array with let for immutable and var for a mutable array. For an immutable array, we have to provide the ... Read More

What is the difference between Static func and Class func in Swift?

Nitin Aggarwal
Updated on 03-Jan-2023 10:04:13

8K+ Views

You will learn about the differences between static and class functions in this article. Many times, you might think they are the same thing, but that's not the case. Let's understand the difference between them. What are Static Functions? In Swift, a static function is a type of function that is associated with a type and not with an instance. This means that an instance is not required to call a static function as it can be called from the type itself. Example Here's an example of how you might define a static function in Swift − struct ViewPoint { ... Read More

NSNotificationCenter addObserver in Swift

Nitin Aggarwal
Updated on 03-Jan-2023 10:00:48

2K+ Views

The purpose of this article is to explain how an iOS application can send and receive change events using NSNotificationCenter. In an iOS application, you might need to send and receive events anywhere in the app. It may be useful to use the NSNotificationCenter class when you need to receive events anywhere in the app. In this case, you can listen to events and react accordingly. NSNotificationCenter class in the Foundation framework that provides a mechanism for broadcasting notifications to registered observers. It allows objects to communicate with each other and respond to events that occur within a program. How ... Read More

What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?

Nitin Aggarwal
Updated on 02-Jan-2023 14:29:11

4K+ Views

This error is very frequent as you can see many times while writing code. Don't worry, you will understand this error in detail in this article. Before jumping to the cause of this error, let's understand what is optional in Swift. Optional Initially, when we receive a value from another source, such as the backend server, it might contain a valid value or nothing at all. In that case, you use optionals that represent a variable with two possible situations: either a value or no value at all. var possibleNumber = "450" var convertedNumber = Int(possibleNumber) In the above ... Read More

Swift: print() vs println() vs NSLog()

Nitin Aggarwal
Updated on 02-Jan-2023 14:30:46

1K+ Views

In this article, you will learn about logging methods in the Swift language. You will also learn what the differences between them are. Debugging is the most common practice while writing code for an iOS application. It enables you to debug logic, code, errors, etc. Swift provides in-built libraries to print logs on the console. We have some options to print logs on the console like print(), println(), and NSLog(). Let's try to understand each of them. print() In Swift, print() is a function that prints a message to standard output (e.g., the console). It takes one or more arguments, ... Read More

Loading/Downloading an image from a URL in Swift

Nitin Aggarwal
Updated on 02-Jan-2023 14:25:57

5K+ Views

In this article, you will learn how you can download an image from a URL in the Swift language. In the iOS application, image downloading from the image URL is the most common task. Apple provides us with a native library to download any data from any URL. There are many third-party libraries available on GitHub to download the images. But in this tutorial, we are not going to use any third-party library. We will use the URLSession class provided by Apple itself. What is the URLSession Class? URLSession is a class in the Foundation framework that provides an API ... Read More

How to find the index of a list item in Swift?

Nitin Aggarwal
Updated on 02-Jan-2023 13:40:14

11K+ Views

Swift gives you some methods to perform on the collection type to get the index of a particular object. To find the index of an item in an array in Swift, you can use the firstIndex(of:) method of the Array type. This method returns the index of the first element in the array that is equal to the given element or nil if no such element is found. How to print the Index of List items? Let's look at an example. Algorithm Step 1 − Define an input array. Step 2 − Call the firstIndex() method on the input array. ... Read More

Advertisements