Programming Articles - Page 409 of 3363

What is the Swift equivalent of respondsToSelector?

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

1K+ 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

697 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

5K+ 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

324 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

2K+ 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

3K+ 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

New Array from Index Range Swift

Nitin Aggarwal
Updated on 11-Apr-2023 10:53:41

1K+ Views

In Swift, you can create an array using the ArraySlice type. The following examples will show how to use the ArraySlice type. You can create an extension as well to define the subscript method. Example 1 In the following example, we create an array of numbers with values from 0 to 9 and then create a range of indices from startIndex to endIndex (exclusive). Using this range we can generate an ArraySlice of the original array. In the end, we will convert the ArraySlice to an array using the Array initializer and print the result. import Foundation let numbers = ... Read More

Multiple Type Constraints in Swift

Nitin Aggarwal
Updated on 11-Apr-2023 10:50:39

1K+ Views

In Swift, there are several ways to implement type constraints on types. We will use some common approaches like the where clause, protocol, etc. Generics provide lots of flexibility to write better and more secure code in Swift. We can apply generics to collections, custom types, etc. One of them is generic type constraints. Using type constraints, you can make your generic code behave that matches a certain set of constraints whatever you define. Swift provides multiple ways to specify type constraints on generic type parameters. Type constraints using the "where" clause The "where" clause in Swift is a highly ... Read More

Java Program to Concatenate Two List

Bharti Kumari
Updated on 31-May-2024 16:15:26

9K+ Views

Introduction In Java, a list is an ordered collection of elements that allows duplicates. Sometimes, we may need to combine two lists into one list. Concatenating two lists means joining the elements of the two lists to form a new list containing all the elements of both lists. There are various ways to concatenate two lists in Java, including using loops, streams, and built-in methods. In this context, we will explore different approaches to concatenate two lists in Java. One approach is to use the addAll() method provided by the List interface. This method adds all the elements of one ... Read More

Java program to compute the sum of numbers in a list using while-loop

Bharti Kumari
Updated on 21-Oct-2024 17:42:02

722 Views

In this article, we will learn to compute the sum of numbers in a list using Java with a while-loop. We will look at two examples: one that sums a list of integers and another that sums a list of double values. For each example, we will create an ArrayList to store the numbers, and then use a while-loop to iterate through the list. During the iteration, we will add each number to a variable called "sum, " which will keep track of the total. This will help you understand how to use loops and ArrayLists in Java effectively. Different ... Read More

Advertisements