Fyg*_*ygo 11 delegates protocols objective-c ios swift
我正在尝试为JSON加载委托创建一个协议JSONLoaderDelegate.我的另一个类叫做JSONLoader,应该将事件分派给它的委托(实现JSONLoaderDelegate协议),如:
self?.delegate?.jsonLoaderdidEndWithError(self!, error: JSONLoaderError.LoadError)
JSONLoader的实现并不重要(imho).但是我似乎有问题来实现协议,这是代码:
@objc protocol JSONLoaderDelegate {
    optional func jsonLoaderdidBeginLoading(jsonLoader: JSONLoader)
    func jsonLoaderdidEndWithError(jsonLoader: JSONLoader, error: JSONLoader.JSONLoaderError)
    func jsonLoaderDidEndWithSuccess(jsonLoader: JSONLoader)
}
这看起来很简单,但我收到一个错误:
方法不能标记为@objc,因为参数的类型不能用Objective-C表示.
指出了这三个功能.
显然,如果我删除@objc属性我不能使用optional该功能.我真的想保留jsonLoaderdidBeginLoading作为可选的.任何想法/方法来解决这个问题?谢谢!
我遇到了这个问题,因为我有一个MyConnection没有父类的课程.像这样.
public class MyConnection {
}
我不能@objc在此之前提出警告
只有从NSObject继承的类才能声明为@objc
所以我改变了继承的类NSObject.像这样
public class MyConnection: NSObject {
}
我有同样的错误。method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C.
我在 swift 2.0 中将语法更改为以下内容。现在效果很好!
@objc protocol AppModelDelegate {
    optional func compititorList(com:[Competitor]!)
}
class Competitor:NSObject{
    var id:Int?
    var title:String?
}
注意:Competitor 类类型更改为 NSObject,这清除了委托规则违规
同样在 NSJSON 解析的情况下。我改为以下内容。
if (_httpStatusCode == 200) {
    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
    var error : NSError?
    print("responseString : \(responseString) ")
    var service_result:NSDictionary =  NSDictionary()
    do {
        let anyObj = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
        service_result = anyObj
    } catch let error as ErrorType {
        print("json error: \(error)")
    }
}
这 3 种方法的共同点是参数JSONLoader,我认为这就是阻止您桥接协议的原因。为了解决这个问题,你必须使其与 objc 兼容。
| 归档时间: | 
 | 
| 查看次数: | 15094 次 | 
| 最近记录: |