• Swift3.0相对于2.3语法的一些变化


    前言 : Swift3.0的Swift的第3个主要版本,目标是安全,快速和有表现力,也是第一个有开源社区参与开发的Swift版本。由于语法和API改动比较多,Xcode 8.0 Beta提供了migrate迁移工具。这样自有的代码升级Swift3.0就比较方便了,但是,关键是要等第三方开源库升级到Swift3.0啊。

    那就一起来看看Swift3.0都有哪些改变吧

    大家都知道Swift诞生在Objective-C已经发展的相当成熟的情况下,为了保证ObjC开发人员顺利过渡到Swift,也因为Swift处于初级阶段,很多类库和方法命名都尽量和ObjC保持一致,在使用Swift开发iOS应用中处处可以看到ObjC的影子。但是作为一门Modern语言Swift还是做出了改变,从中可以看出日后Swift将彻底摆脱ObjC的影子。这其中包括重新导入Foundation消除类型前缀、方法名去重、函数和方法去C风格等等。

    命名的改变

    去掉C语言的风格

    其它变化

    获取屏幕的bounds

    2.3中为:UIScreen.mainScreen().bounds
    3.0中变为:UIScreen.main.bounds

    获取屏幕宽度

    2.3 : UIScreen.mainScreen().bounds.width
    3.0 : UIScreen.main.bounds.width

    获取颜色 后面直接跟颜色的单词即可

    2.3:  UIColor.orangeColor()
    3.0:  UIColor.orange

    KVC字典转模型方法的改变

    2.3 :  setValuesForKeysWithDictionary(dict)
    3.0 :  setValuesForKeys(dict)

    在2.3中的任意对象 AnyObjcet 在3.0中变为 Any

    注册cell方法的改变 省略了一个Nib单词 同理注册class的cell 省略class单词

    2.3 :   tableView.registerNib(UINib(nibName: "XTLiveTableViewCell", bundle: nil), forCellReuseIdentifier: ID)
    3.0 :   tableView.register(UINib(nibName: "XTLiveTableViewCell", bundle: nil), forCellReuseIdentifier: ID)

    设置字体

    2.3 :  UIFont.systemFontOfSize(17)
    3.0 :  UIFont.systemFont(ofSize: 17)

    获取文字宽度

    2.3 :  (title! asNSString) .sizeWithAttributes(nameAttr).width
    3.0 :  (title! asNSString) .size(attributes: nameAttr).width

    按钮设置文字 和监听按钮点击方法的改变

    2.3 :   btn.setTitle(vc.title, forState: .Normal)
            btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
            btn.setTitleColor(UIColor.redColor(), forState: .Selected)
            btn.addTarget(self, action: #selector(XTBaseViewController.btnClick(_:)), forControlEvents: .TouchUpInside)
    
    3.0 :     btn.setTitle(vc.title, for: UIControlState())            
              btn.setTitleColor(UIColor.gray, for: UIControlState())            
              btn.setTitleColor(UIColor.red, for: .selected)            
              btn.addTarget(self, action: #selector(XTBaseViewController.btnClick(_:)), for: .touchUpInside)

    动画方法的改变

    2.3 :  UIView.animateWithDuration(0.25, animations:{}
    3.0 :  UIView.animate(withDuration: 0.25, animations: {}

    生成cell

    2.3 :  let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath)
    3.0 :  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ID, for: indexPath)

    在3.0中cell的indexPath的类型(原来为NSIndexPath) 变为IndexPath

    在使用的时候要转为NSIndexPath再去使用

    2.3 :  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}
           let vc  = childViewControllers[indexPath.row]
    3.0 :  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {}
           let vc  = childViewControllers[(indexPath asNSIndexPath).row]

    获取mainBundle方法的改变

    2.3 :  NSBundle.mainBundle()
    3.0 :  Bundle.main

    CGPointZero在3.0中改为 CGPointzero

    图片的内容模式 .Center 在3.0 中改为 .center

    hidden属性在3.0 中改为 isHidden

    selected属性在3.0中改为 isSelected

    写入文件writeToFile 在3.0中改为write

    声明私有的成员变量和方法的关键字 从Private 改为 fileprivate

    分页属性的从pagingEnabled 变为 isPagingEnabled

    设置URL 从 NSURL(string: string) 变为 URL(string: string)

    设置UICollectionView的布局方法

    2.3 :  overridefunc prepareLayout() {
        super.prepareLayout()    }
    3.0 : overridefunc prepare() {
        super.prepare()    }

    dismiss掉控制器

    2.3 : dismissViewControllerAnimated(false, completion: nil)
    3.0 : dismiss(animated: false, completion: nil)

    通知方法的改变

    2.3 :  NSNotificationCenter.defaultCenter().postNotificationName("loginClick", object: nil)
    3.0 :  NotificationCenter.default.post(name: Notification.Name(rawValue: "loginClick"), object: nil)

    定义枚举 枚举值只能用小写

    2.3 :  enum RequestType {
         case GET
         case POST
         }
    3.0 : enum RequestType {
         case get
         case post
         }

    获取一个控件的最大Y或X值的方法

    2.3 :  CGRectGetMaxY((imageView?.frame)!)
    3.0 :  (imageView?.frame)!.maxY

    获取UIApplication方法

    2.3 :    //改变状态栏的颜色
            UIApplication.sharedApplication().statusBarStyle = .LightContent
    3.0 :   UIApplication.shared.statusBarStyle = .lightContent

    设置形变

    2.3 :  loginView.transform = CGAffineTransformMakeScale(0, 0)
    3.0 :  loginView.transform = CGAffineTransform(scaleX: 0, y: 0)

    总结

    Swift的每次变化由于对之前的版本乃至上一个版本都不兼容造成每次Swift的升级都显得比较虐心,但是事实上这也是Swift的重大进步。记得之前曾有传闻说Swift3.0的语法和API都会稳定并且向上兼容,但是不久这个消息就破灭了,WWDC上官方也再次证实这个希望可能要到4.0才能实现。但是试想一下:Apple在很短的时间内就固话API对于Swift的发展真的是好事吗?毕竟新特性的加入、更好的语法优化才能让Swift越来越好!总的来说,如果应用要升级到Swift3.0可能要做不同程度的修改,但是这种改动仅仅是语法和SDK的变动并不会消耗太多的工作量,更何况Apple提供了迁移工具。

  • 相关阅读:
    深入理解Azure自动扩展集VMSS(1)
    使用ARM和VMSS创建自动扩展的web集群
    使用ARM模板部署自动扩展的Linux VMSS(2)
    使用ARM模板部署自动扩展的Linux VMSS(1)
    Azure上A/D系列虚拟机到DS系列迁移(2)
    ORM进阶操作
    Django之中间件
    restful十项规范
    同源策略与跨域请求
    Django之CSRF问题
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5942253.html
Copyright © 2020-2023  润新知