@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes

Protocol SomeProtocol {
    func registerForPresetLoadedNotification()
}


extension SomeProtocol {

    func registerForPresetLoadedNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self as AnyObject,
                                                         selector: #selector(presetLoaded(_:)),
                                                         name: kPresetLoadedNotificationName,
                                                         object: nil)
    }

    func presetLoaded(notification: NSNotification) {

    }
}

 

Swift 中, 如上定义一个协议, 协议中有一个方法, 通过扩展实现协议方法registerForPresetLoadedNotification, 在该方法中又调用
presetLoaded方法, 此时会报错 Argument of '#selector' refers to a method that is not exposed to Objective-C, 提示需要在方法前添加 @objc, 添加后又报错 @objc can only be used with members of classes, @objc protocols, and concrete extensions of classes,提示让删除@objc.Stack Overflow 上的链接: https://stackoverflow.com/questions/38980887/protocol-extension-on-an-objc-protocol因为 Swift protocol Extension 中的方法对 OC 不可见, 所以不能使用 selector 方法, 解决方法可以使用block 方法, 或参考:

https://www.jessesquires.com/blog/avoiding-objc-in-swift/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值