Found 33676 Articles for Programming

Java Program to Get Year from Date

Shiva Keerthi
Updated on 11-Apr-2023 14:15:57

4K+ Views

In our day to day life, there is a huge data being generated in this real world. Among the information generated, Dates are generally used for various things such as scheduling an appointment, planning for day-to-day activities. While working with dates in programming, extracting the year from a specific date can be a common task. We can extract the Year from a particular Date using different in-built functions in Java programming language. In this article we will be discussing different approaches for extracting year from a particular date. Example Input: "13-07-2000" Output: 2000 Example Input: "1997/07/13" Output: 1997 ... Read More

Java program to get Size of Directory

Shiva Keerthi
Updated on 28-Jan-2025 14:57:26

859 Views

A group of files combined stored at a common location is known as a “Directory”. A directory not only contains a group of files but also it can contain a group of other directories as well. A directory is also known as a “Folder”. Directories are a fundamental component of file systems and are used by different operating systems to provide a logical organization of files and to help us perform file management tasks such as searching, sorting, and moving files. In this section, we are going to write a Java program to get the size of a directory. What is ... Read More

Why is the Convenience Keyword Even Needed in Swift?

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

701 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

581 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

645 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

288 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

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