Where usage in Swift – onlinecode

Where usage in Swift – onlinecode

In this post we will give you information about Where usage in Swift – onlinecode. Hear we will give you detail about Where usage in Swift – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Where is a powerful keyword within Swift to easily filter out values. It can be used in many different variants from which most of them are listed in this post.

Usage in a switch

Consider having the following enum:

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.
enum Action {
    case createUser(age: Int)
    case createPost
    case logout
}

Using where you can easily filter the case for a specific age range:

func printAction(action: Action) {
    switch action {
    case .createUser(let age) where age < 21:
        print("Young and wild!")
    case .createUser:
        print("Older and wise!")
    case .createPost:
        print("Creating a post")
    case .logout:
        print("Logout")
    }
}

printAction(action: Action.createUser(age: 18)) // Young and wild
printAction(action: Action.createUser(age: 25)) // Older and wise

Usage in a for loop

Printing even numbers using a for-loop.

let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers where number % 2 == 0 {
    print(number) // 0, 2, 4, 6, 8, 10
}

Usage in protocol extensions

Extending an Array type based on its element.

extension Array where Element == Int {
    func printAverageAge() {
        let total = reduce(0, +)
        let average = total / count
        print("Average age is (average)")
    }
}

let ages = [20, 26, 40, 60, 84]
ages.printAverageAge() // Average age is 46

Usage in first

Get a first element based on a condition.

let names = ["Henk", "John", "Jack"]
let firstJname = names.first(where: { (name) -> Bool in
    return name.first == "J"
}) // Returns John

Usage in contains

Determine if an array contains a condition matching element.

let fruits = ["Banana", "Apple", "Kiwi"]
let containsBanana = fruits.contains(where: { (fruit) in
    return fruit == "Banana"
}) // Returns true
Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

Usage in initialisers

Conditional initialisers to only allow for certain types.

extension String {
    init(collection: T) where T.Element == String {
        self = collection.joined(separator: ",")
    }
}

let clubs = String(collection: ["AJAX", "Barcelona", "PSG"])
print(clubs) // prints "AJAX, Barcelona, PSG"
 

Hope this code and post will helped you for implement Where usage in Swift – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US