MobileEntityLogo.png

Hi.

Welcome to the best online resource for IOS & Android tutorials.

Separating vowels from constants in Swift

To separate constants from vowels we will create a function that returns a tuple of two values vowels, and constants. in our function we will have a switch statement inside of a advanced for loop to get each element of the string which we pass as parameter for our function. We create a case for each vowel, inside these cases we will append to a variable the current letter found. Then in default we will assign current letter to our constants.


func getvowels(string:String) -> (constants:String, vowels:String) {
    var Constants:String = ""//Stores constants
    var Vowels:String = ""//Stores Vowels
    for letter in string {//Iterate over string
        switch letter.lowercased() {
        case "a":
            Vowels.append(contentsOf: "\(letter)")
        case "e":
            Vowels.append(contentsOf: "\(letter)")
        case "i":
            Vowels.append(contentsOf: "\(letter)")
        case "o":
            Vowels.append(contentsOf: "\(letter)")
        case "u":
            Vowels.append(contentsOf: "\(letter)")
        default://If no vowels are found add remaining characters to constants string
            Constants.append(contentsOf: "\(letter)")
        }
    }
    return ("\(Constants)", "\(Vowels)")//Return tuple
}

Now you will see that when we print Hello World we get the appropriate result.


print(getvowels(string: "Hello World"))
/*
 This is what we get back from the console
 (constants: "Hll Wrld", vowels: "eoo")
 */

Combining protocols to make for more readable code

How to store custom fonts in SwiftUI

How to store custom fonts in SwiftUI

0