Swift Articles

Page 5 of 40

Swift Program to convert the string into an array of characters based on the specified character

Ankita Saini
Ankita Saini
Updated on 10-May-2023 334 Views

In Swift, convert the string into an array of characters based on the specified character using the split() function. The split() function splits the given string at the specified separator and returns the result in an array. Input String = “Today is cloudy day” Character = “y” Output [“toda”, “is cloud”, “da”] Here, the string is split at character “y” and converted the split string into an array. Syntax func.split(separator:Character, maxSplits Int, ommittingEmptySequence:Bool) The split function takes the following arguments − separator − It is an element at which the split operation happens. maxSplits − ...

Read More

Swift Program to Sort a Dictionary By Values

Ankita Saini
Ankita Saini
Updated on 09-May-2023 1K+ Views

Swift supports a sorted() method to sort all the elements present in the given dictionary. This method sorts the key-value pairs of the dictionary according to the values. Syntax func sorted(by:) Here, the value of by parameters are − Greater than(>) − To sort the elements into descending order. Less than(

Read More

Swift Program to Search an Element in a Dictionary

Ankita Saini
Ankita Saini
Updated on 09-May-2023 3K+ Views

In a Swift dictionary, we can search for an element that is either a key or value with the help of contains() function. The contains() function will return a boolean value which represents whether the specified element(key or value) is found or not. Syntax dict.contains{$0.key == “KeyName”} Here, dict is the dictionary. The contains() function returns a boolean value which represents the result. Here we pass a closure in the function which will check if the current key is equal to the specified key or not and then return the result accordingly. Algorithm Step 1 − Create a ...

Read More

Swift Program to Replace Elements in a Dictionary

Ankita Saini
Ankita Saini
Updated on 09-May-2023 427 Views

In Swift, a dictionary is an unordered collection of key-value pairs. So, to replace the value of a key Swift provides bracket notation or subscript notations. Using this notation we can also add new key-value pair in the dictionary if the specified pair does not exist. Syntax dict[keyName] = “Value” Here, dict is the dictionary. The keyName represents the key whose value you want to replace, and the Value represent the new value. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Display the original dictionary. Step 3 − Now replace the value of ...

Read More

Swift Program to Remove Null from a Dictionary

Ankita Saini
Ankita Saini
Updated on 09-May-2023 933 Views

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

Swift Program to Remove duplicate elements from Dictionary

Ankita Saini
Ankita Saini
Updated on 09-May-2023 963 Views

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

Swift Program to Remove all the elements from Dictionary

Ankita Saini
Ankita Saini
Updated on 09-May-2023 493 Views

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

Swift Program to Print even length words

Ankita Saini
Ankita Saini
Updated on 09-May-2023 193 Views

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

Swift Program to Pass Dictionary as the function argument

Ankita Saini
Ankita Saini
Updated on 09-May-2023 1K+ Views

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

Swift Program to merge two integer arrays without using library function

Ankita Saini
Ankita Saini
Updated on 09-May-2023 349 Views

In Swift, we can merge two or more integer arrays without using the library function. So Swift provides an addition assignment(+=) operator to merge two integer arrays. Using this operator we will merge two arrays and assign the result into a new array. Syntax newArray += array Here, newArray is the resultant array and array represents the array which we want to merge. Algorithm Step 1 − Create a function that takes two arrays as an argument and returns a merged array. Step 2 − Inside the function create an empty array to store the resultant array. Step ...

Read More
Showing 41–50 of 397 articles
« Prev 1 3 4 5 6 7 40 Next »
Advertisements