Swift Articles

Page 2 of 40

How do I change the font size of a UILabel in Swift?

Nitin Aggarwal
Nitin Aggarwal
Updated on 07-Sep-2023 3K+ Views

You can change the font size of a UILabel in Swift by setting the font property of the UILabel to a UIFont object with the desired point size. Here is the code for the basic setup import UIKit class TestController: UIViewController { private let messageLabel = UILabel() override func viewDidLoad() { super.viewDidLoad() initialSetup() } ...

Read More

How do I add 1 day to an NSDate?

Nitin Aggarwal
Nitin Aggarwal
Updated on 07-Sep-2023 442 Views

In this article, you will learn how to add the number of days to a date in the Swift language. Many times, you might need to modify the date by changing some number of days, weeks, months, etc. Swift provides an in-built concept for adding a component to date. Let's see some examples. Let's take a brief look at a few of the common concepts that will be used in these examples. Calendar Class The Calendar class in Swift is a class that represents a calendar, which is a system for organizing dates. It is used to perform calendar-related calculations, ...

Read More

Getting a \"This application is modifying the autolayout engine from a background thread\" error?

Nitin Aggarwal
Nitin Aggarwal
Updated on 07-Sep-2023 241 Views

In iOS development, this error frequently occurs while working on user interfaces. Also, you can reduce the chances of coming across this error if you write code carefully. Let's understand what this error is all about. It will help you to understand the reason behind this error if you read the error statement. The error "This application is modifying the autolayout engine from a background thread" is caused when an application attempts to make changes to the user interface from a background thread, which is not allowed in iOS development. Autolayout is a system for defining the layout of user ...

Read More

Get the class name of an object as a string in Swift

Nitin Aggarwal
Nitin Aggarwal
Updated on 07-Sep-2023 2K+ Views

This article will explain to you how to get the name of the class of an object in the Swift language. Swift provides us with a function called type(of:) to get the type of a value or the class name of an object. You can use the type(of:) function to find the dynamic type of a value, particularly when the dynamic type is different from the static type. The static type of a value is the known, compile-time type of the value. The dynamic type of a value is the value’s actual type at run-time, which can be a subtype ...

Read More

Generate a UUID on iOS from Swift

Nitin Aggarwal
Nitin Aggarwal
Updated on 06-Sep-2023 1K+ Views

This article will explain to you what is the UUID in iOS and how to generate it in the Swift language. You will see some practical situations in which UUID is useful in the iOS application. What is UUID? A universally unique identifier (UUID) is a string of characters that is guaranteed to be unique across all devices and at all times. UUIDs are often used to uniquely identify resources, such as database records or files. In iOS, UUIDs can be used for a variety of purposes. Some common use cases for UUIDs in iOS include − Identifying app-specific ...

Read More

Swift Program to Check if a String is Numeric

Ankita Saini
Ankita Saini
Updated on 19-Jul-2023 2K+ Views

To check if the given string is numeric or not we use Double() initializer in Swift Programming. A string is an ordered collection of characters for example “Sky”. A string can be numeric and non-numeric. A numeric string is a string which only contains numbers for example “12345”. So the valid numeric strings are: “3423”, “222333”, “34.342”, etc., whereas non-valid numeric strings are “23hfd23”, “423131sdd”, etc. So it converts the given string into double and return true if the given string is numeric otherwise return false. Algorithm Step 1 − Create a function Step 2 − Return ...

Read More

Swift Program to get the flattened 1D array

Ankita Saini
Ankita Saini
Updated on 19-Jul-2023 695 Views

Swift has various functions like flatmap(), compactmap(), reduce() to flatten an 1D array. An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. Method 1: Using flatmap() Function To flatten 2D array into 1D array we can use flatmap() function. The flatmap() function return an array by concatenating the sub arrays into a single array. Syntax func flatMap{_res} Here, res is a closure which accepts the element of the specified array as its argument and return a flattened ...

Read More

Swift Program to get the subarray from an array using a specified range of indices

Ankita Saini
Ankita Saini
Updated on 19-Jul-2023 2K+ Views

In swift, we have various methods like Array() initializer and for-in loop that can be used for getting the subarray from an array using a specified range of indices. An array stores elements of the same data type in an order. Let’s understand both the methods in detail in this article. Method 1: Using Array() initialiser To get the subarray from an array using a specified range of indices we uses Array() initialiser. Here we find the subarray using range operator and then convert the elements into the array using Array() initialiser. Algorithm Step 1 − Create an ...

Read More

Swift Program to Use Different Types of a Collection

Ankita Saini
Ankita Saini
Updated on 16-Jun-2023 275 Views

A collection is a group of elements or objects that are gathered together for some specific tasks. Swift supports three types of collections: Array, Set, and Dictionary. They are implemented as generic collections, also they are clear about what type of values they can store which means you can not store the wrong type of values in the collections. Array It is an ordered collection which is used to store similar types of data or elements. It can store duplicate values. It is both mutable and immutable. Syntax var arr :[Type] = [E1, E2, E3] var arr = ...

Read More

Swift Program to Replace the Spaces of a String with a Specific Character

Ankita Saini
Ankita Saini
Updated on 16-Jun-2023 1K+ Views

In Swift, we are allowed to replace whitespaces with the specified character like $, *, ! etc. So, to replace the spaces of a string with a specific character swift provide the following methods − Using replacingOccurrences() method Using user-defined method Using components() and joined() methods Using split() and joined() methods Using map() and joined() method Method 1: Using the replacingOccurrences() method The replacingOccurrences() method is used to create a string in which all the occurrences of the target string or character are replaced by the specified string or character. Syntax func replacingOccurrences(of: String, with: String) ...

Read More
Showing 11–20 of 397 articles
« Prev 1 2 3 4 5 40 Next »
Advertisements