美文网首页
Object-C -> Swift

Object-C -> Swift

作者: br_MorningStar | 来源:发表于2017-12-14 16:52 被阅读7次

通知 Notification

  • sendMessage
 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        NotificationCenter.default.post(name:NSNotification.Name(rawValue: "playState"), object: true)
    }
  • getMessage
NotificationCenter.default.addObserver(self, selector: #selector(isPlayer(notification:)), name: NSNotification.Name(rawValue: "playState"), object: nil)


    @objc func isPlayer(notification : NSNotification){
        let isPlay = notification.object as! Bool
    }

闭包

  • 定义
/*参数 返回值 */
    /*var complectionCallback : (channel : GiftChannelView) -> Void 保证初始化所以是Optional*/
    /*var complectionCallback : ((channel : GiftChannelView) ->Void)? 3.0不能写外部参数*/
    var complectionCallback : ((GiftChannelView) ->Void)?
  • 调用 调度
complectionCallback(self)
  • 在别的地方接受
 channelView.complectionCallback = { channelView in

            }
  • 参数形式
   class func tabBarControllerWithAddChildVCsBlock(bblock : (_ tabbar : MainTabarController) -> ()) -> MainTabarController{
        bblock(.shareInstance)
        return  .shareInstance
    }
  • 参数形式调用
MainTabarController.tabBarControllerWithAddChildVCsBlock { (<#MainTabarController#>) in
            <#code#>
        }

KVC

  • 在Swift 4.0中使用KVC总是调用下面的方法
override func setValue(_ value: Any?, forUndefinedKey key: String) {
        print(key)
    }

原因是 : 因为在Swift 4中继承 NSObject 的 swift class 不再默认全部 bridge 到 OC。也就是说如果我们想要使用KVC的话我们就需要加上@objcMembers 这么一个关键字。

引用: 在 swift 3 中除了手动添加 @objc 声明函数支持 OC 调用还有另外一种方式:继承 NSObject。class 继承了 NSObject 后,编译器就会默认给这个类中的所有函数都标记为 @objc ,支持 OC 调用。苹果在Swift 4 中苹果修改了自动添加 @objc 的逻辑:一个继承 NSObject 的 swift 类不再默认给所有函数添加 @objc。只在实现 OC 接口和重写 OC 方法时才自动给函数添加 @objc 标识。

所以我们在调用KVC的时候才不能被赋值。因为KVC是OC的方法
感谢您的博客 http://blog.csdn.net/ZCMUCZX/article/details/78218643

相关文章

网友评论

      本文标题:Object-C -> Swift

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