Found 517 Articles for Swift

Swift Program to Implement Bubble Sort Algorithm

Ankita Saini
Updated on 21-Apr-2023 11:32:18

341 Views

In swift, Bubble sort algorithm is the easiest search algorithm. This algorithm sorts the elements by repeatedly swapping adjacent elements if they are not present at the right place. This algorithm works well only for small set of elements, it is not suitable for larger number of elements because its average and worst case time complexity is high. So the working of the bubble sort is − Suppose we have the following array − Now we sort the array in ascending order using the bubble sort. Here starting from the first index we compare the first and second element. ... Read More

Swift Program to Implement Binary Search Algorithm

Ankita Saini
Updated on 13-Apr-2023 11:43:30

1K+ Views

In swift, Binary search algorithm is used to search element from the sorted array. It repeatedly divides the search interval into half and then search the specified element. In the binary search algorithm, the input array must be in sorted order to reduce the time complexity. Algorithm Step 1 − Create a function to implement a binary search algorithm. Step 2 − Inside the function, first we find the lower bound and upper bound range of the given array. Step 3 − Run a while loop till LBound

Why is the Convenience Keyword Even Needed in Swift?

Nitin Aggarwal
Updated on 11-Apr-2023 11:30:08

498 Views

In Swift, you can create an additional initializer to provide default values for the properties. You can use the convenience keyword to add this functionality. Let's look at some examples of how to use a convenience initializer in the Swift language. What is a convenience initializer in Swift? In Swift, a secondary initializer in a class that provides extra or alternative ways to create an instance of that class is marked with the convenience keyword. The initialization procedure is streamlined and made simpler, which makes it easier for the developer to deal with the class. The designated initializer of the ... Read More

What's the equivalent of NSLocalizedString in Swift?

Nitin Aggarwal
Updated on 11-Apr-2023 11:27:26

433 Views

In real iOS applications, you often need to support localization to ensure the app's accessibility around the world. By incorporating localization into your app, you can gain more users. In Swift, we use the NSLocalizedString function to create a localized string. What is localization? Localization is the process of allowing various language support in your apps. Instead of utilizing the software in a single language, it helps to create a more localized experience for users. Localization will be quite simple to integrate in your application. Apple offers a totally native method for integrating localization in your program.Syntax The syntax for ... Read More

What is the Swift equivalent of respondsToSelector?

Nitin Aggarwal
Updated on 11-Apr-2023 11:43:47

780 Views

In Swift, the equivalent of the Objective-C method respondsToSelector is the responds property of the NSObject class. To check if an object responds to a particular selector, you can use the responds(to:) method which is declared in the NSObjectProtocol. Here's the syntax − if objectName.responds(to: #selector(methodName)) { // do something if the object responds to methodName } else { // do something else if the object doesn't respond to methodName } In this syntax, objectName is the object that you want to check, and methodName is the selector that you want to check ... Read More

What is the most succinct way to remove the first character from a string in Swift?

Nitin Aggarwal
Updated on 11-Apr-2023 11:18:36

372 Views

In Swift, we can use methods like dropFirst, Index(after:), and many others that can remove the first character of a string. In this article, we are going to learn various examples of these functions and understand how can we remove the first character of a string. Using the dropFirst method In this method, we are going to use the dropFirst method to remove the first character from the string and make the new string start with the second character. In case the original string is empty or has a single character in it, the result will be an empty string.Example ... Read More

Swift: Pass an array by reference?

Nitin Aggarwal
Updated on 11-Apr-2023 11:14:18

3K+ Views

In Swift, you can pass an array by reference in a function as an argument using inout keyword. In Swift, arrays are value types by default. In other words, they pass a value rather than a reference. If you pass an array to a function as an argument, it makes a copy and passes that copy to the function. First, let's understand what happens when we pass an array in a function as a value. func passByValue(_ array: [Int]) { array.append(100) } let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] passByValue(numbers) ... Read More

Swift JSONDecode decoding arrays fail if single element decoding fails

Nitin Aggarwal
Updated on 11-Apr-2023 11:09:18

179 Views

In Swift, working with JSON objects is very easy with the JSONDecoder class. It is always necessary to create model classes or structs with the Codable protocol. One single mistake leads you to fail to decode the complete JSON object. Let's explore some examples to understand when this failure might occur and how you can handle it in Swift. What is the JSONDecoder class? The JSONDecoder class is then used to parse the JSON data from the file into an instance of the given Type either a class or structure. The decode(_:from:) method is used to deserialize the JSON data, ... Read More

Overriding a superclass property with a different type in Swift

Nitin Aggarwal
Updated on 11-Apr-2023 11:05:37

1K+ Views

We can define a subclass that is inherited from a superclass in the Swift language, That means you can override its properties and methods using the override keyword. However, you cannot override the properties with a different type in the subclass. By default, the subclass must override the properties of the same type as the superclass type. For example, let's say you have a Person class with name and age properties of type String and Double − class Person { let name: String let age: Double ... Read More

Objective-C and Swift URL encoding

Nitin Aggarwal
Updated on 11-Apr-2023 11:01:18

2K+ Views

URL encoding is an essential part of iOS app development. Many times, you will need to encode the URL to communicate with servers. Let's look at some examples of how you can encode an URL in Swift and Objective-C. URL Encoding in Swift In Swift, you can use the addingPercentEncoding(withAllowedCharacters:) method for URL encoding. URL Encoding in Objective-C In Objective-C, you can use the stringByAddingPercentEncodingWithAllowedCharacters method for URL encoding. Encode a URL in Objective-C In Objective-C, you can encode a URL string using the stringByAddingPercentEncodingWithAllowedCharacters: method of the NSString class. Here's an example NSString *stringToEncode = @"https://www.exampleserver.com/complete path with spaces"; ... Read More

Advertisements