[英]Type 'X' does not conform to protocol 'ResponseObjectSerializable'
I try to make custom response serialization with Alamofire 我尝试使用Alamofire进行自定义响应序列化
I follow what written in README and create protocol and extension 我遵循README中的内容并创建协议和扩展
@objc public protocol ResponseObjectSerializable {
init?(response: NSHTTPURLResponse, representation: AnyObject)
}
extension Alamofire.Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, NSError?) -> Void) -> Self {
let serializer: Serializer = { (request, response, data) in
let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
if response != nil && JSON != nil {
return (T(response: response!, representation: JSON!), nil)
} else {
return (nil, serializationError)
}
}
return response(serializer: serializer, completionHandler: { (request, response, object, error) in
completionHandler(request, response, object as? T, error)
})
}
}
but when I try to conform it I got this error Type 'my_model_class' does not conform to protocol 'ResponseObjectSerializable' 但是当我尝试遵守它时我得到了这个错误类型'my_model_class'不符合协议'ResponseObjectSerializable'
My model is just a bare-bone class 我的模型只是一个裸骨类
final class Shot: ResponseObjectSerializable {
required init?(response: NSHTTPURLResponse, representation: AnyObject) {
}
}
Use this with Xcode 6.3, anyone experienced this? 与Xcode 6.3一起使用,有谁经历过这个? and know how to make this work? 并知道如何使这项工作?
Response to @airspeed The error go away, but what confusing me is in Apple Swift document they have an example on @objc
protocol and the swift class that conform it doesn't need @objc
对@airspeed的回应错误消失了,但令我困惑的是Apple Swift文档中有一个关于@objc
协议的例子和符合它的swift类不需要@objc
@objc protocol CounterDataSource {
optional func incrementForCount(count: Int) -> Int
optional var fixedIncrement: Int { get }
}
class TowardsZeroSource: CounterDataSource {
func incrementForCount(count: Int) -> Int {
if count == 0 {
return 0
} else if count < 0 {
return 1
} else {
return -1
}
}
}
Shot
isn't marked as @objc
, unlike the protocol, so your init
doesn't match the requirement: 与协议不同, Shot
未标记为@objc
,因此您的init
与要求不匹配:
@objc public protocol ResponseObjectSerializable {
init?(response: NSHTTPURLResponse, representation: AnyObject)
}
final class Shot: ResponseObjectSerializable {
@objc required init?(response: NSHTTPURLResponse, representation: AnyObject) {
}
}
results in the error: 导致错误:
note: protocol requires initializer
init(response:representation:)
with type(response: NSHTTPURLResponse, representation: AnyObject)
注意:协议需要初始化程序init(response:representation:)
与类型(response: NSHTTPURLResponse, representation: AnyObject)
init?(response: NSHTTPURLResponse, representation: AnyObject)` ^
note: candidate is not
@objc
, but protocol requires it 注意:候选人不是@objc
,但协议需要它
Stick an @objc
in front of the definition of Shot
and it should compile. 将@objc
在Shot
的定义之前,它应该编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.