Found 517 Articles for Swift

Swift Program to get the imaginary part of the given complex number

Ankita Saini
Updated on 09-Jan-2023 14:48:54

76 Views

In this article, we will learn how to write a swift program to get the imaginary part of the given complex number. Complex numbers are those number which express in the form of x+yi, where x and y are real numbers and ‘i’ is an imaginary number known as iota. For example: 1+2i, 1-3i, etc. Here we use the following methods to get imaginary part − Using function Without using function Method 1: Using function To get the imaginary part of the complex number we create a function which takes struct object as a parameter and return the ... Read More

Swift Program to get the denominator from a rational number

Ankita Saini
Updated on 09-Jan-2023 14:46:37

202 Views

In this article, we will learn how to write a swift program to get the denominator from the rational number. A rational number is a number, which represents in the form of n/m where m is not equal to zero. Here n is known as the numerator and m is known as the denominator. For example, 2/3, 11/19, etc. Here, 2 and 11 are numerators whereas 3 and 19 are denominators. Algorithm Step 1 − Create a structure to create a rational number. Step 2 − In this structure, create two properties of integer type to store the numerator and ... Read More

Swift Program to Copy All the Elements of One Array to Another Array

Ankita Saini
Updated on 09-Jan-2023 14:44:28

3K+ Views

In this article, we will learn how to write a swift program to copy all the elements of one array to another array. Swift doesn’t provide a direct method to copy the elements of one array to another but we can do this using the following methods − Using map(_:) method Using = operator Using append(contentsOf:) method Method 1: Using map(_:) method The map(_:) method is used to return an array that contains the result of mapping the given closure over the specified sequence’s elements. Syntax func map(_mclosure:(Self.Element) throws - >T)rethrows [T] Here, mclouser is a mapping ... Read More

Swift Program to Check If a Number is Spy number or not

Ankita Saini
Updated on 09-Jan-2023 14:40:19

340 Views

In this article, we will learn how to write a swift program to check if a number is a spy number or not. A spy number is a number in which the sum of the digits of the given number is equal to the product of the digits of the given number. For example − Number = 123 Sum = 1 + 2 + 3 = 6 Product = 1 * 2 * 3 = 6 Hence 123 is a spy number because sum = product Number = 23 Sum = 2 + 3 = 5 Product = 2 * ... Read More

Swift program to check if a given square matrix is an Identity Matrix

Ankita Saini
Updated on 09-Jan-2023 14:36:29

154 Views

In this article, we will learn how to write a swift program to check if a given square matrix is the identity matrix. The identity matrix is an MxM square matrix in which the main diagonal consists of ones and other elements are all zero. For example − $\mathrm{M\:=\:\begin{bmatrix}1 & 0 & 0 & 0ewline0 & 1 & 0 & 0 ewline0 & 0 & 1 & 0 ewline0 & 0 & 0 & 1\end{bmatrix}}$ Whereas a square matrix is a matrix in which the number of rows is equal to number of columns and it may or may not ... Read More

Swift Program to Add Two Matrix Using Multi-dimensional Arrays

Ankita Saini
Updated on 09-Jan-2023 14:34:18

330 Views

In this article, we will learn how to write a swift program to add two matrices using a multi-dimensional array. A matrix is a mathematical structure in which the elements are present in rows and columns format. For example, the first element is present at the a00 location, the second at a01, and so on. Therefore, to add two matrices we are going to use + operator to add the elements of two matrices like a00 + b00 and then store the sum into a new matrix. For example: Matrix 1 − $\mathrm{\begin{bmatrix}2 & 3 & 4 ewline5 & 2 ... Read More

Precision String Format Specifier In Swift

Nitin Aggarwal
Updated on 03-Jan-2023 10:32:41

891 Views

It is sometimes necessary to format strings in a custom format in order to use them in an application. For example, you can format product prices, decimal-point numbers, etc. In Swift, you can use precision specifiers to specify the number of decimal places or the number of characters to be displayed in a string. Example 1 To specify the number of decimal places for a floating-point number, you can use the %.nf format specifier, where n is the number of decimal places., import Foundation let productPrice = 300.3456789 let formattedPrice = String(format: "%.2f", productPrice) print("The formatted price is: ", formattedPrice) ... Read More

How to use a Background Thread in Swift?

Nitin Aggarwal
Updated on 03-Jan-2023 10:26:51

6K+ Views

In this article, you will learn how you can perform a task in the background using a background thread in the Swift language. Swift provides us with several ways to perform background tasks. One popular option of theirs is GCD (generally called Grand Central Dispatch), which is a low-level API for managing concurrently in the Swift language. The GCD provides us with a global queue for creating background threads. You can invoke the DispatchQueue.global() method to get an instance of a global dispatch queue. Using the same instance, you can use the async() method to execute a block of code ... Read More

How to define Optional Methods in the Swift Protocol?

Nitin Aggarwal
Updated on 03-Jan-2023 10:25:17

838 Views

This article will explain to you how to define optional methods in the protocol. Before diving into making optional methods in the protocol, you will first learn what a protocol is and how to declare one in Swift. What is The Protocol? A protocol is a type that defines a group of methods or properties. Basically, you can define a blueprint of methods to specify behavior. A protocol is similar to interfaces in other programming languages. Syntax Here is the syntax of a simple protocol in Swift − protocol { // Properties // ... Read More

How do I get the version and build number of an application using Swift?

Nitin Aggarwal
Updated on 03-Jan-2023 10:22:21

5K+ Views

You often need to deal with the version and build number of the iOS application. It helps you to perform debugging operations on the server side. You can check which version or build the client (IOS app user) has. Based on that, you can debug the issues coming from the client side. Version and build numbers might be needed to display to the user to verify what version and build are currently running by the end user. To get the app version and build number in Swift, you can use the Bundle class, which provides access to the app's bundle ... Read More

Advertisements