Found 517 Articles for Swift

Swift Program to delete prefix substring from the given string

Ankita Saini
Updated on 09-May-2023 10:24:21

490 Views

To delete the prefix substring from the given string first, we check if the given substring is present in the specified string or not using the inbuilt hasPrefix() function. Then find the index of the prefix substring using the inbuilt index() function and finally remove the prefix substring. Input String = “Today is cloudy day” SubString = “Today” Output “is cloudy day” Here, the specified substring is found in the given string, so in the resultant string, we removed the substring from the start of the input string. Algorithm Step 1 − Create a string. Step 2 ... Read More

Swift Program to Create an Empty Dictionary

Ankita Saini
Updated on 09-May-2023 10:21:44

434 Views

A dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers of any data type like string, integer, etc., that are connected with each value. In Swift, we are allowed to create an empty dictionary. An empty dictionary is a dictionary which does not contain any element or we can say that its size is zero. Syntax let myDicti = [Int:Int]() Or let myDicti:[Int:Int]= [:] Or let myDicti:[String:Int]= [:] So to create an empty dictionary you can use any of the given syntaxes. Here it ... Read More

Swift Program to count a specified character inside the string

Ankita Saini
Updated on 09-May-2023 10:18:20

874 Views

In Swift, to count how many times a specified character occurs in the given string we create a function. It will increase the value of the counter by one whenever the specified character appears in the given string and then return the final count at the end of the given string. Input String = “Today is cloudy day” Character = “y” Output 3 Where, character “y” appears three times in the given string so the output of the input is 3. Algorithm Step 1 − Create a function that returns the specified character's total occurrence. Step 2 ... Read More

Swift Program to convert the array of characters into the string

Ankita Saini
Updated on 09-May-2023 10:08:02

512 Views

To convert the array of characters into a string Swift provides the following methods − Using String() initialiser Using append() method Input array = [“S”, “w”, “i”, “f”, “t”] Output Swift Here we join all the characters given in the array to create a string. Method 1: Using String() Initialiser To convert the array of characters into a string we can use String() initialisers. The String() initialiser will create the object of a string. Here we use parameterise string initialiser. Syntax Swift(value) Here value represents the array which we want to convert into a string. ... Read More

Swift Program to check the given string is empty or not

Ankita Saini
Updated on 09-May-2023 10:03:05

1K+ Views

To check if the given string is empty or not Swift provides an inbuilt property named as isEmpty. This property will return true if the given string is empty. And return false if the given string is not empty. Here empty string means whose length is 0 or we can say a string that does not contain any character. String 1 = “Swift” String 2 = “” Where string 1 is not an empty string because it contains a sequence of characters or it has some length. Whereas string 2 is an empty string because it does contains anything ... Read More

Swift Program to Check if two Dictionaries are equal

Ankita Saini
Updated on 09-May-2023 09:59:28

317 Views

In Swift, a dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers. So to check the equality of two dictionaries we first need to check if the size of both the dictionaries is equal or not. If yes, then we check if both dictionaries contain the same key-value pairs or not. If both conditions are equal, then that means the given two dictionaries are equal. If any of the conditions are false then that means the given dictionaries are not equal. Algorithm Step 1 − Create ... Read More

Swift Program to check a string starts with a specified substring

Ankita Saini
Updated on 09-May-2023 09:56:17

886 Views

Swift provides an hasPrefix() function to check if a string starts with a specified substring. The hasPrefix() function returns a boolean value which indicates whether the specified substring matches the starting string characters of the input string. The hasPrefix() function is case sensitive, which means according to this function “a”, and “A” are two different values. Input String = “Ram got first place” SubString = “Ram” Output Yes Here, the characters of the substring match the starting characters of the input string. Syntax func hasPrefix(value) Here, the value represents a string. This function returns true if the ... Read More

Swift Program to check a string end with a specified substring

Ankita Saini
Updated on 09-May-2023 09:54:35

441 Views

Swift provides an hasSuffix() function to check a string end with a specified substring. The hasSuffix() function returns a boolean value which indicates whether the specified substring matches the ending characters of the input string. The hasSuffix() function is case sensitive that means according to this function “t”, and “T” are two different values. Input String = “Today is cloudy day” SubString = “day” Output yes Here, the character of the substring matches the ending characters of the input string. Syntax func hasSuffix(value) Here, the value represents a string. This function returns true if the value matches ... Read More

Swift Program to Add Elements to a Dictionary

Ankita Saini
Updated on 09-May-2023 09:49:31

75 Views

In Swift, a dictionary is an unordered collection in which data is stored in the form of key-value pairs. To add elements to a dictionary we can use any of the following methods − Using Bracket notation Using updateValue() method Method 1: Using Brackets Notation We can key-value pairs in the dictionary using bracket notation or we can say that using subscript notation. In this method, we can add a new key in the brackets and assign the corresponding value. If the specified key already exists, then it will replace the value of that key we the new ... Read More

ViewDidAppear is not called when opening the app from the background

Nitin Aggarwal
Updated on 04-May-2023 13:00:46

828 Views

In Swift, the viewDidAppear method does not call when an application is opened from the background. In that case, you have to use the applicationWillEnterForeground method in the AppDelegate. In this article, you will see an example of how to perform an action when opening an app from the background. Approach If you open an app from the background, it should call the applicationWillEnterForeground method of the app delegate before presenting the view. However, it's possible that the viewDidAppear method of the view controller is not called if the view controller's view is already on the screen or if the ... Read More

Previous 1 ... 6 7 8 9 10 ... 52 Next
Advertisements