Found 34494 Articles for Programming

How to iterate a loop with index and element in Swift?

Nitin Aggarwal
Updated on 07-Sep-2023 14:25:38

876 Views

In this article, you will learn how to iterate a collection using a loop with an index and element in the Swift language. In this article, you will learn how to use the enumerated() method. In Swift, you can use the enumerated() method to iterate over the elements of a collection and access both the index and the element in each iteration of the loop. enumerated() enumerated() is a method in Swift that allows you to iterate over the elements of a collection, such as an array or a dictionary. It returns a series of tuple elements, each of which ... Read More

How to check if an element is in an array?

Nitin Aggarwal
Updated on 07-Sep-2023 14:10:43

148 Views

This article will explain how you can check if an element exists in an array or not in the Swift language. There are several ways to check if an element is in an array in Swift − Using the contains method The contains(_:) method returns true if an array contains the target element. This method can only be used with arrays whose elements conform to the Equatable protocol. Here is an example where we use contains with an array of strings. String conforms to the Equatable protocol, so we can use the contains method here. Algorithm Step 1 - ... Read More

How do I shuffle an array in Swift?

Nitin Aggarwal
Updated on 07-Sep-2023 09:14:06

162 Views

In this article, you will learn how to shuffle an array in the Swift language. In Swift, you can use the shuffle() and shuffled() methods to shuffle the array elements. shuffle() The shuffle() method shuffles the elements of the collection in place, so the original array is modified. shuffled() If you want to create a shuffled copy of the array instead of modifying the original array, you can use the shuffled() method from the Sequence protocol. Here is an example of how to use shuffle() to shuffle the array elements Algorithm Step 1 - Create an input array Step ... Read More

How do I make an attributed string using Swift?

Nitin Aggarwal
Updated on 07-Sep-2023 09:10:50

338 Views

This article will explain how you can make an attributed string in the Swift language. What are the steps to apply different attributes to a string in Swift? In Swift, we use the NSAttributedString class to make an attributed string. In Swift, NSAttributedString is a class used to create and manage attributed strings. An attributed string is a string that has additional attributes, such as text color, font, and style, applied to parts of the string. This article will show different use cases for an attributed string. Basic Setup import UIKit class TestController: UIViewController { private ... Read More

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

Nitin Aggarwal
Updated on 07-Sep-2023 08:58:53

1K+ 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
Updated on 07-Sep-2023 08:55:29

117 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
Updated on 07-Sep-2023 08:50:34

73 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
Updated on 07-Sep-2023 08:49:13

984 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
Updated on 06-Sep-2023 18:14:04

606 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

Golang program to implement a weighted interval scheduling algorithm

Akhil Sharma
Updated on 05-Sep-2023 19:03:01

126 Views

The Weighted Interval Scheduling Problem revolves around a set of intervals, each having an associated weight. In this article, we will implement the Weighted Interval Scheduling algorithm in go, using two methods: Recursive and Dynamic Programming. This classic optimization problem involves selecting non-overlapping intervals with maximum total weight. Explanation Recursive Method The Recursive Method takes a straightforward yet elegant approach. It examines each interval one by one and considers two scenarios − whether to include the current interval or skip it. This method utilises recursion to explore all possible combinations of intervals, calculating the maximum weight. While ... Read More

Advertisements