Thursday, October 13, 2016

Migrating to Swift 2.3 or Swift 3 from Swift 2.2


Xcode 8 has feature to Migrator from 2.2, 2.3 ----> 3.0

Take care of these steps before migrate

1. All Build and test case should be pass on Xcode7.3
2. Project should be have version control otherwise you are able to see the changes after the migration.
3. If your project contains multiple scheme than better to go for two different projects.

Swift 3.0 Migration

When you the xcode 8.0, it will prompted the alert for migration from Swift 2.2 or 2.3 to Swift 3.
Same thing you can do manually also from  edit --> convert ---> To convert swift syntax

Somebody has issue like me,  We wanted to stay 2.3 for time been and release the build from Xcode 8.

Solution is very easy just you have to set "Use legacy Swift build setting" ---> YES 

you'll able to build the project and run into iOS10 devices.


Swift 3.0 Migration Issues

There are many issues come across to you.. like compiler crash or syntax error.. If you don't want to this kind issue than follow above steps

Well there are known issues in Swift 3.0 migration 

1. Swift Standard library
   Migrator failed due to using of indexing methods on SetIndex and DictionaryIndex 


Learn Swift 3.0 in 5 hours





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()