encodable

Swift 4 - Cannot invoke 'encode' with an argument list of type '(Codable)'

佐手、 提交于 2019-12-02 03:22:37
问题 I have built a set of API functions which encode an object (using a Struct which conforms to Codable ), then Posts the resulting JSON Data object to a server, then decodes the JSON response. All works fine - especially happy with the new method for JSON parsing in Swift 4.2. However, now I want to refactor the code so that I can reuse the code for various method calls - when I do I get a really annoying error. func encodeRequestJSON(apiRequestObject: Codable) -> Data { do { let encoder =

Swift 4 - Cannot invoke 'encode' with an argument list of type '(Codable)'

杀马特。学长 韩版系。学妹 提交于 2019-12-02 01:06:41
I have built a set of API functions which encode an object (using a Struct which conforms to Codable ), then Posts the resulting JSON Data object to a server, then decodes the JSON response. All works fine - especially happy with the new method for JSON parsing in Swift 4.2. However, now I want to refactor the code so that I can reuse the code for various method calls - when I do I get a really annoying error. func encodeRequestJSON(apiRequestObject: Codable) -> Data { do { let encoder = JSONEncoder() let jsonData = try encoder.encode(apiRequestObject) let jsonString = String(data: jsonData,

Store Encodables in a Swift Dictionary

时光毁灭记忆、已成空白 提交于 2019-11-30 09:33:33
问题 I'm looking to store models objects in a Dictionary and would like to serialize the whole dictionary using JSONEncoder into data and subsequently into a string and save it. The idea is to use Swift 4's out of the box Encodable to ensure anything that I add to the dictionary will be serialized which can include primitives and custom objects (which will themselves conform to Encodable ). The Challenge is what type should I declare the dictionary to be: If I use [String: Any] , it won't know how

Why can not use protocol `Encodable` as a type in the func

一曲冷凌霜 提交于 2019-11-28 11:12:24
I'm trying to get data by encode model which conforms to Encodable protocol. But it's failed to invoke func encode like code below: // MARK: - Demo2 class TestClass2: NSObject, Encodable { var x = 1 var y = 2 } var dataSource2: Encodable? dataSource2 = TestClass2() // error: `Cannot invoke 'encode' with an argument list of type '(Encodable)'` let _ = try JSONEncoder().encode(dataSource2!) //func encode<T>(_ value: T) throws -> Data where T : Encodable But in another demo, it works well, why? // MARK: - Demo1 protocol TestProtocol { func test() } class TestClass1: NSObject, TestProtocol { func

How to use Any in Codable Type

喜夏-厌秋 提交于 2019-11-27 08:39:43
I'm currently working with Codable types in my project and facing an issue. struct Person: Codable { var id: Any } id in the above code could be either a String or an Int . This is the reason id is of type Any . I know that Any is not Codable . What I need to know is how can I make it work. Codable needs to know the type to cast to. Firstly I would try to address the issue of not knowing the type, see if you can fix that and make it simpler. Otherwise the only way I can think of solving your issue currently is to use generics like below. struct Person<T> { var id: T var name: String } let

Why can not use protocol `Encodable` as a type in the func

夙愿已清 提交于 2019-11-27 05:58:52
问题 I'm trying to get data by encode model which conforms to Encodable protocol. But it's failed to invoke func encode like code below: // MARK: - Demo2 class TestClass2: NSObject, Encodable { var x = 1 var y = 2 } var dataSource2: Encodable? dataSource2 = TestClass2() // error: `Cannot invoke 'encode' with an argument list of type '(Encodable)'` let _ = try JSONEncoder().encode(dataSource2!) //func encode<T>(_ value: T) throws -> Data where T : Encodable But in another demo, it works well, why?

How to use Any in Codable Type

拜拜、爱过 提交于 2019-11-26 13:56:31
问题 I'm currently working with Codable types in my project and facing an issue. struct Person: Codable { var id: Any } id in the above code could be either a String or an Int . This is the reason id is of type Any . I know that Any is not Codable . What I need to know is how can I make it work. 回答1: Codable needs to know the type to cast to. Firstly I would try to address the issue of not knowing the type, see if you can fix that and make it simpler. Otherwise the only way I can think of solving