New features in Swift 3.0


Document:Apple Swift Document
Release Notes:Official release notes
import UIKit

class ViewController: UIViewController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // 修改1:traditional C-style for loop is deprecated
        for i in 0..<3
        {
            print(i)
        }

        // ps. for var i = 3; i > 0; i-- {} can be write like blow:
        for i in (0..<3).reverse()
        {
            print(i)
        }

        // ps. for var i = 0; i < 10; i+=2 {} can be write like blow:
        for i in 0.stride(to: 10, by: 2)
        {
            print(i)
        }

        // 修改2: selector will be checked in compile-time
        let tap = UITapGestureRecognizer(target: self, action: #selector(selectorFunc))
        view.addGestureRecognizer(tap)

        // 修改3: Swift version-check will be in compile-time
        #if swift(>=2.2)
            print("Swift 2.2+")
        #else
             This code will never be checked!!!!!!!!
            print("Swift 2.2-")
        #endif

        // 修改4:
        testFunc("kuang", age: 25)

        // 修改5:can compare two tuples
        let tupleOne = (firstName:"asahi", age:25)
        let tupleTwo = (first:"asahi", a:25)
        if tupleOne == tupleTwo {
            print("equal")
        }
        else {
            print("not equal")
        }

        // 修改6: tuple splate syntax is deprecated.
        /*
        let parameterTuple = ("asahi", age:25)
        testFunc(parameterTuple)
         */

        // 修改7:var parameters is deprecated
        var name = "Tylor Swift"
        parametersFunc(name)
        parameterFuncNew(&name)

        // 修改8:debug identifiers are renamed
        debugFunc()
    }

    func selectorFunc()
    {
        print("selector function")
    }

    func testFunc(name: String, let age: NSInteger)
    {
        print(name+"\n",age)
    }
    /**
     *  @brief 'var' parameters are deprecated and will be removed in Swift 3.0
     */
    func parametersFunc(var name: String)
    {
        name = name.uppercaseString
        print(name)
    }

    func parameterFuncNew(inout name: String)
    {
        name = name.uppercaseString
        print(name)
    }

    func debugFunc()
    {
        // old version
        /*
        print(__FUNCTION__, __LINE__, __FILE__, __COLUMN__)
        */

        // new version
        print(#function, #line, #file, #column)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}