Found 33676 Articles for Programming

How to use typeof operator in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:56:15

1K+ Views

In TypeScript, the typeof is one of the most helpful operators and keywords to check the types of variables or identifiers. However, TypeScript is a type-strict language. So, we need to define the type of the variable or object while defining the identifier itself. Still, sometimes we need to check the type of the variable. For example, we are taking the data from the user via forms and handling it via TypeScript. Then we have to validate the data and perform some database operations. Also, the identifier can have multiple types in TypeScript. So, using the typeof operator, we can ... Read More

How to specify optional properties in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:53:53

1K+ Views

We will learn to specify the option properties in TypeScript. The true meaning of the optional property is that properties can be undefined or null, and we can initialize them whenever required. In real-time development, the importance of optional properties is very much. For example, we are fetching the data from the API and performing some operations on the data. If you try to use the data without getting it due to the database server being down or if there is any other problem, it will raise an error. In such case, we can make the data property optional and ... Read More

How to get substring in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:51:17

722 Views

The string contains various characters, and the substring is the part of the string. We can get the substring from the string two ways. The first is using the starting and ending position of the substring, and the second is using the starting position and length of the substring. We will learn both ways to get the substring from the string in TypeScript in this tutorial. Get The Substring Using The Start Position and End Position In TypeScript, indexing of string starts from zero. So, if we have a zero-based starting and ending position of the substring, we can get ... Read More

How to find the natural logarithm of a number in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:48:34

630 Views

The natural logarithm is the logarithm of any numeric value to the base e. Here e is Euler's constant, and the value of Euler’s constant is approximately 2.718. In TypeScript, we can use the built-in library methods to find the natural logarithm of any numeric value greater than or equal to zero. Using the Math.log() Method Math is a library of TypeScript which contains all the methods to perform mathematical operations. Inside the Math object, all methods are static. So, we can directly access all methods by taking Math (object name) as a reference. The math method also contains the ... Read More

How to create an array of objects in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:46:04

16K+ Views

In TypeScript, the array contains the data or different values and can also contain an object. The object contains the properties and methods in TypeScript. We can access the object properties and invoke the object method by taking the object as a reference. In this tutorial, we will learn to create an array of multiple objects in TypeScript. We will also learn to perform some operations, such as sorting on the array of objects. Syntax Here, we have given the syntax to follow to create the array of objects. let obj_array: Array = [{object properties}] In the above syntax, ... Read More

Precision String Format Specifier In Swift

Nitin Aggarwal
Updated on 03-Jan-2023 10:32:41

2K+ Views

It is sometimes necessary to format strings in a custom format in order to use them in an application. For example, you can format product prices, decimal-point numbers, etc. In Swift, you can use precision specifiers to specify the number of decimal places or the number of characters to be displayed in a string. Example 1 To specify the number of decimal places for a floating-point number, you can use the %.nf format specifier, where n is the number of decimal places., import Foundation let productPrice = 300.3456789 let formattedPrice = String(format: "%.2f", productPrice) print("The formatted price is: ", formattedPrice) ... Read More

How to use a Background Thread in Swift?

Nitin Aggarwal
Updated on 03-Jan-2023 10:26:51

8K+ Views

In this article, you will learn how you can perform a task in the background using a background thread in the Swift language. Swift provides us with several ways to perform background tasks. One popular option of theirs is GCD (generally called Grand Central Dispatch), which is a low-level API for managing concurrently in the Swift language. The GCD provides us with a global queue for creating background threads. You can invoke the DispatchQueue.global() method to get an instance of a global dispatch queue. Using the same instance, you can use the async() method to execute a block of code ... Read More

How to define Optional Methods in the Swift Protocol?

Nitin Aggarwal
Updated on 03-Jan-2023 10:25:17

1K+ Views

This article will explain to you how to define optional methods in the protocol. Before diving into making optional methods in the protocol, you will first learn what a protocol is and how to declare one in Swift. What is The Protocol? A protocol is a type that defines a group of methods or properties. Basically, you can define a blueprint of methods to specify behavior. A protocol is similar to interfaces in other programming languages. Syntax Here is the syntax of a simple protocol in Swift − protocol { // Properties // ... Read More

How do I get the version and build number of an application using Swift?

Nitin Aggarwal
Updated on 03-Jan-2023 10:22:21

6K+ Views

You often need to deal with the version and build number of the iOS application. It helps you to perform debugging operations on the server side. You can check which version or build the client (IOS app user) has. Based on that, you can debug the issues coming from the client side. Version and build numbers might be needed to display to the user to verify what version and build are currently running by the end user. To get the app version and build number in Swift, you can use the Bundle class, which provides access to the app's bundle ... Read More

How do I convert a Swift array to a string?

Nitin Aggarwal
Updated on 03-Jan-2023 10:18:55

3K+ Views

Let's look at some examples of how to convert an array to a string. Method 1:Using Joined(seperator:) Syntax Swift provides us with a method joined(separator:) of an array that can be used to convert a Swift array to a string. This method returns a new string by concatenating the elements of the array, separated by the provided separator string. let wordInString = words.joined(separator: ", ") In order to use the joined() method, call it by array along with passing the separator whatever you want. Algorithm Step 1 − Initialize your array Step 2 − Call joined() method with ... Read More

Advertisements