扩展基础类以符合SWIFT协议 我试图定义一个协议,我想将它与几个基础类以及我自己的一些自定义类型相一致。我第一次尝试在协议中使用一个方便的初始值设定项,但是没有。我在Apple dev论坛上读到了关于使用返回Self类型的类方法的文章,但我不知道该怎么做 typealias JSONObject = AnyObject protocol JSONParseable { static func fromJSONObject(jsonObject: JSONObject) throws -> Self } extension NSURL: JSONParseable { class func fromJSONObject(jsonObject: JSONObject) throws -> Self { guard let jsonString = jsonObject as? String else { throw JSONParseError.ConversionError(message: "blah") } guard let result = NSURL(string: jsonString) else { throw JSONParseError.ConversionError(message: "blah") } return result // Error: cannot convert return expression of type 'NSURL' to return type 'Self' } } 我找到了一个答案,但答案是把这个班定为最后一个——我显然不能在基础课上这样做。
有人能解释一下如何修正我上面的方法吗?或者提出一种不同的方法来增加对基础类的协议一致性?< p>使用代码>自已.init(…)<代码>,而不是<代码> nSURL(…)< />代码,因为这也需要用于<代码> NSURL< /Cord>子类。扩展基础类以符合SWIFT协议 我试图定义一个协议,我想将它与几个基础类以及我自己的一些自定义类型相一致。我第一次尝试在协议中使用一个方便的初始值设定项,但是没有。我在Apple dev论坛上读到了关于使用返回Self类型的类方法的文章,但我不知道该怎么做 typealias JSONObject = AnyObject protocol JSONParseable { static func fromJSONObject(jsonObject: JSONObject) throws -> Self } extension NSURL: JSONParseable { class func fromJSONObject(jsonObject: JSONObject) throws -> Self { guard let jsonString = jsonObject as? String else { throw JSONParseError.ConversionError(message: "blah") } guard let result = NSURL(string: jsonString) else { throw JSONParseError.ConversionError(message: "blah") } return result // Error: cannot convert return expression of type 'NSURL' to return type 'Self' } } 我找到了一个答案,但答案是把这个班定为最后一个——我显然不能在基础课上这样做。,swift,protocols,foundation,Swift,Protocols,Foundation,有人能解释一下如何修正我上面的方法吗?或者提出一种不同的方法来增加对基础类的协议一致性?< p>使用代码>自已.init(…),而不是 nSURL(…)< />代码,因为这也需要用于 NSURL< /Cord>子类。 protocol JSONParseable { static func fromJSONObject(jsonObject: JSONObject) throws -> Self } extension NSURL : JSONParseable { c
protocol JSONParseable {
static func fromJSONObject(jsonObject: JSONObject) throws -> Self
}
extension NSURL : JSONParseable {
class func fromJSONObject(jsonObject: JSONObject) throws -> Self {
guard let jsonString = jsonObject as? String else {
throw JSONParseError.ConversionError(message: "blah")
}
guard let result = self.init(string: jsonString) else {
throw JSONParseError.ConversionError(message: "blah")
}
return result
}
}
protocol JSONParseable {
typealias T = Self
static func fromJSONObject(jsonObject: JSONObject) throws -> T
}
extension NSURL : JSONParseable {
typealias T = NSURL
class func fromJSONObject(jsonObject: JSONObject) throws -> T {
guard let jsonString = jsonObject as? String else {
throw JSONParseError.ConversionError(message: "blah")
}
guard let result = NSURL(string: jsonString) else {
throw JSONParseError.ConversionError(message: "blah")
}
return result
}
}
Hack:定义一个
typealias T
,这将返回一个NSURL
,即使对于子类也是如此
protocol JSONParseable {
static func fromJSONObject(jsonObject: JSONObject) throws -> Self
}
extension NSURL : JSONParseable {
class func fromJSONObject(jsonObject: JSONObject) throws -> Self {
guard let jsonString = jsonObject as? String else {
throw JSONParseError.ConversionError(message: "blah")
}
guard let result = self.init(string: jsonString) else {
throw JSONParseError.ConversionError(message: "blah")
}
return result
}
}
protocol JSONParseable {
typealias T = Self
static func fromJSONObject(jsonObject: JSONObject) throws -> T
}
extension NSURL : JSONParseable {
typealias T = NSURL
class func fromJSONObject(jsonObject: JSONObject) throws -> T {
guard let jsonString = jsonObject as? String else {
throw JSONParseError.ConversionError(message: "blah")
}
guard let result = NSURL(string: jsonString) else {
throw JSONParseError.ConversionError(message: "blah")
}
return result
}
}
是的,谢谢!我使用self.init(…)方法。如果您喜欢答案,请不要忘记向上投票。