Found 26504 Articles for Server Side Programming

Smallest triangular number larger than p

Vaishnavi Tripathi
Updated on 11-Apr-2023 16:20:27

368 Views

We will discuss triangular numbers and how we can find the smallest triangular number just larger than a given number “num”. We will first discuss what exactly is a triangular number and then will find out the smallest triangular number just larger than “num” We will see two different approaches for the same. In the first approach, we will run a simple loop to generate the output, while in our second approach, we will first generate a general formula for calculating the desired number and then will directly apply that formula to get the smallest triangular number. Problem Statement We ... Read More

Pandigital Product

Vaishnavi Tripathi
Updated on 11-Apr-2023 16:19:20

351 Views

We are given two numbers and our task is to find out whether the given number is obtained by multiplying two other numbers such that all three numbers together constitute a 9-digit pandigital number. In other words, it can be said that we have to find out whether the given number is pandigital after combining it with two other numbers which result in the original number on multiplication. We can have many such cases where we will get multiple solutions for this problem, to get the best time complexity, we will simply print the first ever solution found and stop ... Read More

Minimum Adjacent Swaps Required to Sort the given Binary Array

Vaishnavi Tripathi
Updated on 11-Apr-2023 16:06:50

919 Views

There are different approaches, we can use to minimize the number of swaps required on adjacent elements to get a sorted array. The given array as the output only contains two types of elements i.e., 0 and 1. We will discuss two different approaches to solve the problem in which the first solution uses extra space to store the number of zeroes, while the second one uses only constant space. Problem Statement We are given an array which contains only two types of elements 0 & 1. Our aim is to find out how many minimum swaps on adjacent elements ... Read More

Why is the Convenience Keyword Even Needed in Swift?

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

704 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

584 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

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

647 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

292 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

Advertisements