记录项目中Swift3.0的语法变化

Swift中CGRect等结构体的变化

之前转载了一篇博客,只要介绍了CGRect,CGSize和CGPoint的Swift写法
[转]Swift 范的 CGRect、CGSize 和 CGPoint

在这里主要记录一下CGRect的变化,我们也可以延伸其他结构体的写法

  /// 官方文档
    public init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat)

    public init(x: Double, y: Double, width: Double, height: Double)

    public init(x: Int, y: Int, width: Int, height: Int)

看到上面的文档我们就知道了怎么写了

let button = UIButton(frame: CGRect.init(x: 0, y: 0, width: 44, height: 44))

Swift3.0中Selector的变化

之前写过一篇博客主要介绍了Swift2.x中Selector的变化
Swift3.0中Selector的变化

    /// Swift3.0中的写法
let button = UIButton(type: .Custom)
button.addTarget(self, action: #selector(bottomButtonClick), for: .touchUpInside) 
func bottomButtonClick(sender: UIButton){
  /// 打印button的tag
}

Swift3.0中SnapKit的新写法

最新项目中使用了OC和Swift的混编,项目中使用了SnapKit,但是使用pod管理时出现问题,但是SnapKit是支持Swift3.0的,在上看到了一个不错的博客swift3.0使用SnapKit3.0.0+

SnapKit之前的写法

        //添加控件
        view.addSubview(bottomButton)
        bottomButton.snp_makeConstraints { (make) in
            make.bottom.equalTo(view.snp_bottom)
            make.left.right.equalTo(view)
            make.height.equalTo(44)
        }```
如果我们还是这样写,xcode8会显示如下提示
![xcode8提示](http://upload-images.jianshu.io/upload_images/1242012-97ccaf2bc4052ed3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![xcode8提示](http://upload-images.jianshu.io/upload_images/1242012-a959208adaaaaf7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
通过提示可以看到请使用`snp.xxx`代替`snp_xxx`

### SnapKit Swift3.0的写法

```Swift
        bottomButton.snp.makeConstraints{ (make) in
            make.bottom.equalTo(view.snp.bottom)
            make.left.right.equalTo(view)
            make.height.equalTo(44)
        }

你可能感兴趣的:(记录项目中Swift3.0的语法变化)