Iterating through Dictionaries
//create a Swift 3 dictionary with values
// just to show we can, lets change the value types,
// so the dictionary type is [String : AnyObject]
var someDict = ["Current Zip Code" : 34050, "Previous Zip Code" : 43256, "Next Zip" : "the moon"]
var someString:String = "" // will use later...
for (theKey, theValue) in someDict {
//iterate through the dictionary until we find the previous zip code
if (theKey == "Previous Zip Code") {
//check what the value type is before doing anything with it
if let possibleNumber:Int = theValue as? Int {
//possibleNumber was successfully created equaling theValue
print (possibleNumber)
}
}
// another approach...
else if (theKey == "Next Zip") {
//check if its a string...
if (theValue is String) {
//it's safe to force someString to equal theValue since
// we know it's definitely a string...
someString = theValue as! String
}
}
}
Logical AND OR operators
//set up a bunch of conditional variables to test in our if statement that contains AND plus OR operators.
let hasDoorKey = false
let knowsOverridePassword = true
let enteredDoorCode = false
let passedRetinaScan = true
// && is an AND operators and checks if BOTH conditions are true
// || is an OR operator, so only one OR condition needs to be true to proceed
if ((enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword) {
//the first condition was false because only one was true (passedRetinaScan)
// the second condition (hasDoorKey) also false
// the third condition knowsOverridePassword was true though
print ("Welcome")
} else {
print ( "DENIED")
}
reference :- http://swift3tutorials.com/
No comments:
Post a Comment