Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Tuesday, October 11, 2016

Swift 3 :– Iterating through Dictionaries & Logical AND OR operators

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/

Swift 3:- For Loops, While Statements, Random Number, Casting & Dictionaries

These are formatted so you can copy the code straight into your Swift Playground files.


// would print up to 9
for i in 0 ..< 10 {
    print (i) //i will increment up one with each iteration of the for loop
}
// would print up to 10
for i in 0 ... 10 {
    print (i)
}
// iterate through an array
let names = ["Bob", "Carl", "Joe", "Dory" ]
for name in names {
    print (name)
}
// nodes in a sprite kit scene (won't work in Playgrounds)
for node in self.children {
    if (node.name == "Ball") {
        //do something
    }
}

While Statements

// The code would run this loop 100 times
var i:Int = 0
while i < 100 {
    print(i)
    i += 1 //increment i so eventually we break out of the while loop
}
// while loops can also be used with non-numbers
var title:String = ""
//this loop will run 5 times
while title != "aaaaa" {
    title = title + "a"  //add another "a" each loop to the title
}

Random Number

//  Make a variable equal to a random number....
let randomNum:UInt32 = arc4random_uniform(100) // range is 0 to 99
// convert the UInt32 to some other  types
let randomTime:TimeInterval = TimeInterval(randomNum)
let someInt:Int = Int(randomNum)
let someString:String = String(randomNum) //string works too

Casting
Dictionaries

//create the Swift 3 Dictionary with nothing, and set the types 
// (String will be Key, and any object will be the value
var someDict = [String : AnyObject]()
// add a key and value
someDict["Ex wives"] = 5
print (someDict)
// add another key and value
someDict["Current wives"] = 1
print (someDict)
//remove Current wives. Oh well.
someDict.removeValue(forKey: "Current wives")
print (someDict)
//changes ex wives to 6
someDict["Ex wives"] = 6
print (someDict)
// remove all
someDict.removeAll()