Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Loop in Apple Swift

Apple's newly released language Swift has an example on the official documentation. Example is like this;

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}
largest

This is pretty simple but as an extra exercise,it requires to add another variable in order to return what type is the largest number (i.e. Square is the case here)

However, I can't seem to figure out what is "(kind,numbers)" here represent and how should I make my for-loop to go through all Dictionary(interestingNumbers) keys and find which key has the largest number.

Thank you all for your help in advance

like image 984
MertD Avatar asked Jun 05 '14 12:06

MertD


People also ask

Can you use for loops in SwiftUI?

It is not possible to use procedural looping mechanisms such as For and While loops to layout views in SwiftUI. The SwiftUI framework provides the ForEach struct to accomplish an alternative to repeatedly add views to a view.

How do I run a string loop in Swift?

To loop or iterate over every character in a String in Swift, we can use for-in statement with String. During each iteration we get the access to this character in the loop body.

How do you exit a for loop in Swift?

You can exit a loop at any time using the break keyword. To try this out, let's start with a regular while loop that counts down for a rocket launch: var countDown = 10 while countDown >= 0 { print(countDown) countDown -= 1 } print("Blast off!")

What is switch statement in Swift?

The switch statement allows us to execute a block of code among many alternatives. The syntax of the switch statement in Swift is: switch (expression) { case value1: // statements case value2: // statements ... ... default: // statements }


1 Answers

Swift allows you to loop over a dictionary with tuple-syntax (key, value). So in every iteration of the for-loop Swift cares about reassigning the specified tuple-variables (kind and number in your case) to the actual dictionary-record.

To figure out which Key includes the highest number in your example you can extend your code as follows:

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]

var largest = 0
var largestKey = ""
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
            largestKey = kind
        }
    }
}
largest     // =25
largestKey  // ="Square"

Or if you want to practice the tuple-syntax try that (with the same result):

var largest = 0
var largestKey = ""
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            (largestKey, largest) = (kind, number)
        }
    }
}
largest     // =25
largestKey  // ="Square"
like image 108
zoma Avatar answered Oct 17 '22 21:10

zoma