SWIFT1.X升级到SWIFT2.0相关记录

吴统威 on 编程语言 SWIFT | 2015-10-08 11:54:36.0

这篇文章用来记录swift2.0的相关改变.陆续会添加......

1.NSRegularExpression

let regex = NSRegularExpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: nil, error: nil)

to

let regex = try! NSRegularExpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: [])


2.count

let sz = count("hello")

to

let sz = "hello".utf16.count


3.viewControllers属性

let currentViewController = pageViewController.viewControllers[0] as UIViewController

to

let currentViewController = pageViewController.viewControllers?[0] as UIViewController


4. "|" 逻辑操作符的升级

let buttonPath = UIBezierPath(roundedRect: button.bounds, byRoundingCorners: UIRectCorner.TopLeft | UIRectCorner.BottomLeft, cornerRadii: CGSizeMake(1.0, 1.0))

to

let buttonPath = UIBezierPath(roundedRect: button.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.BottomLeft], cornerRadii: CGSizeMake(1.0, 1.0))


5.swift2.0后,系统默认请求为https,因此如果使用Http请求会出现以下错误

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."

如果使用http请求需要在Info.plist中加入以下键值

类型
NSAppTransportSecurityDictionary
    NSAllowsArbitraryLoadsBooleanYES


6.Alamofire请求网络数据中调用responseJSON方法返回数据处理

Alamofire.request(.GET, url, parameters: params, encoding: ParameterEncoding.URL).responseJSON { (_, _, result) in
    switch result {
        case .Success(let data):
            let json = JSON(data)
            let name = json["name"].string
        case .Failure(_, let error):
            print("Request failed with error: \(error)")
    }
}