iOS 2.2升级3.0 Swift 各种坑啊 Ambigu

作者: 梁同桌 | 来源:发表于2016-11-16 23:04 被阅读1017次
  • 时隔1个月,终于下定决心把项目从2.2升级为Swift 3.0 ,花费了10左右小时,代码量有5000多一点, 本来0个警告,现在还有32个警告还没有给消除呢。

我总结3.0与2.2区别

  • 1.闭包注意回调函数 + @escaping
    @escaping与@non-escaping声明用来修饰闭包的
    在函数return后,闭包并不会被销毁,它被持有了。因为这个闭包要在异步请求回来后才执行,这时候函数已经return了,为了能执行,必须被其他对象持有,<b>这里需要注意循环引用</b>
    @non-escaping:闭包在函数内执行完后,函数才返回,闭包销毁.

  • 2.函数 名字 _

///根据 用户查询 他上传的歌曲
    class func QueryUserMusicNetGet(_ user: UserModel,net:@escaping (_ objects: [AnyObject]?, _ error: NSError?) -> ()){

        //创建用户
        let user = AVObject(className: "_User", objectId: (user.objectID))
        //创建查询
        let query = MusicNet.query()!
        query.whereKey(LYMusicKeyUserList, equalTo:user)
        
        MusicNet.baseNetGet(query) { (objects, error) in
            net(objects, error)
        }

    }
  • 3 系统属性名字的变化
    1.属性名字开头小写
    2.NS大量取消:Bundle,原来是NSBundle

  • 4.注意
    -open 和 public 定义的 entity 允许被所有作用域(包括当前模块内文件或是其他模块文件)访问;
    -internal 作用范围仅限在 entity 所定义的模块内部,其他模块文件无法访问。ps:默认 Access Control Level 为 Internal;
    -fileprivate 作用范围为当前文件,因此一个文件内定义多个类,某个类标记为 fileprivate 之后,当前模块内的其他文件无法访问这个类,而当前文件内定义的其他类可以访问;
    -private 只允许当前作用域访问。

    -open 只能应用于类和类成员,与 public 的不同之处在于:
    -public 以及其他 more restrictive 访问级别只能在定义的模块内被继承;
    -public 以及其他 more restrictive 访问级别只能在定义的模块内被重写;
    -open 则既可以在定义的模块或是其他模块内被继承或重写

  • 5.函数有返回值必须接收一下(不接收有警告)
    比如

            _=navigationController?.popViewController(animated: true)
  • 6.异步这个代码警告
       ///异步执行
        DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: {
            ///回到主线程
            DispatchQueue.main.async(execute: { () -> Void in
         
            })
            
        })
//下面的不警告
        DispatchQueue.global().async {
            // code
            DispatchQueue.main.sync {
                // 主线程中
            }
        }
  • 7.坐标位置
      CGRectGetMaxX(cove.frame)
      cove.frame.maxY
     //更好使了
  • SDWebImage用不成,错误
 imageView.sd_setImage(with: URL(string: newValue.userModel.headImageUrl), placeholderImage: UIImage(named: "Icon")) { (image, err, type, url) in
            }

什么鬼一直报错 Ambiguous use of 'sd_setImage

  • 找到错误了如下:
    imageView.sd_setImage(with: URL(string: newValue.userModel.headImageUrl), placeholderImage: UIImage(named: "Icon"), options: SDWebImageOptions.retryFailed) { (image, error, type, url) in
                weakSelf.userHeadBtn.setImage(image, for: UIControlState())
            }

Swift,必须要带参数的方法

妈的 归档有严重bug

不知道什么情况

    ///归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(musicName, forKey: "musicName")
        aCoder.encodeDouble(progress, forKey: "progress")
    }
    ///解裆
    required init(coder aDecoder: NSCoder)
    {
        super.init()
        
         musicName=aDecoder.decodeObjectForKey("musicName") as? String
         progress=aDecoder.decodeDoubleForKey("progress")//这句话,就是崩溃。 取不出值纳闷了。
    }

  最后解决方法的思路,就是所有归档都存字符串,先Double先转成字符串,归档,
  解档:取出字符串来以后再转成Double,记得强制解包。

   ///归档
    func encode(with aCoder: NSCoder) {
        aCoder.encode(musicName, forKey: "musicName")
        aCoder.encode("\(progress!)", forKey: "progress")//这里记得加!要不然是可选的
    }
    ///解裆
    required init(coder aDecoder: NSCoder)
    {
        super.init()
        musicName=aDecoder.decodeObject(forKey: "musicName") as! String      
        progress=Double(aDecoder.decodeObject(forKey: "progress") as! String)
      
    }

修复所有我所遇到BUG,另外把警告全部给消除,用15小时左右了。

很爽

个人博客: http://www.liangtongzhuo.com

相关文章

网友评论

    本文标题:iOS 2.2升级3.0 Swift 各种坑啊 Ambigu

    本文链接:https://www.haomeiwen.com/subject/gaorpttx.html