我扩展了RxSwift的Observable,并与Moya一起使用,因此我将其写成这样
/// JSON map to object
/// - returns: Observable<T>
func jsonMapObject<T: EVReflectable>(type: T.Type, forKeyPath: String? = nil) -> Observable<Result<T, ORMError>> {
return self.map { rawResult in
guard let result = rawResult as? Result<JSON, ORMError> else {
return .failure(ORMError.ORMParseJSONError)
}
switch result {
case let .success(json):
guard json != JSON.null,
let dict = json.dictionaryObject else {
return .failure(ORMError.ORMParseJSONError)
}
guard let object: T = self.dictMap(from: dict) as T else {
return .failure(ORMError.ORMCouldNotMakeObjectError)
}
return .success(object)
case let .failure(error):
return .failure(error)
}
}
}
这样编写代码后,我在编辑器中看到一条错误消息
实例方法'dictMap(from:forKeyPath :)'要求'NSObject'符合'EVReflectable'
我不知道如何再次进行,因为此错误不会消失
我的dictMap写为
///
Create the object from the dictionary
internal func dictMap<T: NSObject>(from: NSDictionary?, forKeyPath: String? = nil) -> T where T: EVReflectable{
let instance: T = T()
let parsedObject: T = ((instance.getSpecificType(from ?? NSDictionary()) as? T) ?? instance)
let _ = EVReflection.setPropertiesfromDictionary(from ?? NSDictionary(), anyObject: parsedObject, forKeyPath: forKeyPath)
// if self.statusCode > 300 {
// instance.addStatusMessage(DeserializationStatus.Custom, message: "HTTP status code: \(self.statusCode)")
// }
return parsedObject
}
答案 0 :(得分:0)
您的函数func dictMap<T: NSObject>(from: NSDictionary?, forKeyPath: String? = nil) -> T where T: EVReflectable
要求T为NSObject并符合EVReflectable。为此,NSObject必须符合EVReflectable。
您可能只需要从上面删除: NSObject>
:
func dictMap<T>(from: NSDictionary?, forKeyPath: String? = nil) -> T where T: EVReflectable