Introduction Many times we have seen a case while developing an android application that we have set the same style for multiple views across our application. We have seen writing the code to set the style multiple times. To reduce the code duplication and optimization of our code we can create the style for a specific view in our styles.xml file and set that style to our view programmatically. In this article we will take a look on How to programmatically set the style attribute in a view on Android. Implementation We will be creating a simple application in which ... Read More
Sometimes a dictionary contains null values so to remove those null values Swift provides an in-built function named the filter() method. In the filter() method, we pass a closure which returns a boolean value indicating whether the key-value pair should be included in the resultant dictionary or not. Or we can say that if the second element(value) of a key-value pair is not nil, then it will be included in the resultant dictionary. Otherwise not. Syntax dict.filter{$0.1 != nil} Here, dict is the dictionary. The filter() function return key-value pairs which satisfy the given closure. The closure returns true ... Read More
In Swift dictionary, we cannot have duplicate keys but can have duplicate values hence in this article we will remove duplicate values from the dictionary. So for that, we use contains() function. This function checks if the dictionary contains duplicate values or not. If the dictionary contains duplicate values, then it will remove them from the resultant dictionary. Syntax dict.values.contains(val) Here, dict is the resultant dictionary in which we will store unique key-value pairs and the contains() function compares two values with each other. Algorithm Step 1 − Create a dictionary named as myCityRank with key-value pairs. Step ... Read More
In Swift, we can remove an element from the specified dictionary using the removeValue() function. This function removes the specified key and its associated value from the given dictionary. Syntax Dict.removeValue(forKey: key) Here, the key represents the key which we wanted to remove from the dictionary along with its value, and the dict represents the dictionary. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Print the original dictionary. Step 3 − Remove a key-value pair whose key is 2 using the removeValue() function. Step 4 − Print the updated dictionary. Example In ... Read More
In a dictionary, to remove all the elements from the dictionary Swift provides an inbuilt function named as removeAll() function. It will remove all the key-value pairs from the specified dictionary. Syntax dict.removeAll() Here, dict is the dictionary. The removeAll() function does not take any parameter and removes all the key-value pairs present in the specified dictionary. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Print the original dictionary. Step 3 − Now remove all the key-value pairs from the dictionary using removeAll() function. Step 4 − Print the output. Example In ... Read More
A dictionary is an unordered collection in which data is stored in the form of key-value pairs. To print the keys and values of a dictionary Swift supports the following methods − Using for-in loop Using properties Method 1: Using for-in Loop To print keys and values of the given dictionary we can use a for-in loop. The for-in loop iterates through each pair of the dictionary and displays them on the output screen. Syntax for(key, value) in dict{ print(“\(key) = \(value)”) } Here, the key represents the key and value represents the associated ... Read More
To print even-length words we calculate the length of each word using the count property. Then we check if the length of the word is even or not. If yes, then we will print the word. Otherwise not. Input Str = “Learn Swift language” Output language Here the given string has three words: “Learn", “Swift”, and “language”. But the output is “language” because its length is even that is 8. Algorithm Step 1 − Create a variable to store a string. Step 2 − Split the string into words using the split() function. Step 3 − Now ... Read More
Ever imagined how essential maps and locations are in our life? Just consider finding a restaurant or looking at a particular store. You open up your phone or tablet, and voila! A street map shows you routes and points of interest along those routes. Are you on holiday in a tourist destination? A map will show you locations, routes, roads, points of interest, and crucial buildings such as restaurants and medical shops. However, these are more challenging to access if you know how to get to your current location. Moreover, in case of an emergency or a critical situation, you ... Read More
In Swift, a dictionary is used to create an unordered collection in which the data is stored in the form of key-value pairs. So to print a dictionary we will use the following methods − Using for-in loop Using description property Method 1: Using for-in Loop In Swift, we can print all the key-value pairs of the given dictionary with the help of a for-in loop. The for-in loop iterates through each pair of the dictionary and displays them on the output screen. Syntax for(key, value) in dict { print(“\(key) = \(value)”) } Here, ... Read More
A Swift dictionary is an unordered collection in which data is stored as key-value pairs. So to pass a dictionary as a function argument simply create a function with an argument of dictionary type and a dictionary, then pass it to the function at the time of function calling. Syntax func functionName(dict:[DataType:DataType]){ // Body } So this is how you can define a function which takes a dictionary as its argument. functionName(dict:DictionayName) This is how you can pass a dictionary as a function argument. Dict.last Here Dict is the name of the dictionary from ... Read More