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
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
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
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
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
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
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
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
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
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
 
 Data Structure
 Data Structure Networking
 Networking RDBMS
 RDBMS Operating System
 Operating System Java
 Java MS Excel
 MS Excel iOS
 iOS HTML
 HTML CSS
 CSS Android
 Android Python
 Python C Programming
 C Programming C++
 C++ C#
 C# MongoDB
 MongoDB MySQL
 MySQL Javascript
 Javascript PHP
 PHP