'JSONEncoder' / 'JSONDecoder' is ambiguous for type lookup in this context
我正在关注错误
我不知道为什么得到这个,我该如何解决?
请帮忙!
注意:我正在使用Xcode 9.3.1和Swift4,
我曾尝试使用JSONCodable.JSONEncoder和JSONCodable.JSONDecoder,但无法使用。
这里是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import Foundation import JSONCodable extension JSONEncoder { func encode(_ value: CGAffineTransform, key: String) { object[key] = NSValue(cgAffineTransform: value) } func encode(_ value: CGRect, key: String) { object[key] = NSValue(cgRect: value) } func encode(_ value: CGPoint, key: String) { object[key] = NSValue(cgPoint: value) } } extension JSONDecoder { func decode(_ key: String, type: Any.Type) throws -> NSValue { guard let value = get(key) else { throw JSONDecodableError.missingTypeError(key: key) } guard let compatible = value as? NSValue else { throw JSONDecodableError.incompatibleTypeError(key: key, elementType: type(of: value), expectedType: NSValue.self) } guard let objcType = String(validatingUTF8: compatible.objCType), objcType.contains("\\(type)") else { throw JSONDecodableError.incompatibleTypeError(key: key, elementType: type(of: value), expectedType: type) } return compatible } func decode(_ key: String) throws -> CGAffineTransform { return try decode(key, type: CGAffineTransform.self).cgAffineTransformValue } func decode(_ key: String) throws -> CGRect { return try decode(key, type: CGRect.self).cgRectValue } func decode(_ key: String) throws -> CGPoint { return try decode(key, type: CGPoint.self).cgPointValue } } |
使用模块名称作为前缀,以提示编译器扩展哪个类。
1 2 3 4 5 6 7 8 9 10 | import Foundation import JSONCodable extension JSONCodable.JSONEncoder { // extension code } extension JSONCodable.JSONDecoder { // extension code } |
但是不适用于该特定库,因为该库声明了具有相同名称(
1 2 3 4 5 6 7 8 9 10 11 | import Foundation import class JSONCodable.JSONEncoder import class JSONCodable.JSONDecoder extension JSONCodable.JSONEncoder { // your code } extension JSONCodable.JSONDecoder { // your code } |