codable

Swift 4 JSON Codable - value returned is sometimes an object, others an array

孤街醉人 提交于 2019-11-30 21:31:12
the data I'm getting from an API returns a single object but when there's multiple objects, it returns an array in the same key. With the current model (struct) I'm working with, the decoding fails when an array shows up. These results are randomly ordered, meaning I can't know when I will be served an object or array. Is there a way to create a model that takes this fact into account and can assign the correct type to cast for the value ('String' or '[String]') so that the decoding continues without problem? This is an example of when an object is returned: { "firstFloor": { "room": "Single

Codable and XMLParser in Swift4

那年仲夏 提交于 2019-11-30 18:47:51
Using Swift4, iOS11.1, Xcode9.1, Using the new Swift4 typealiase "Codable" works well for JSON-decoding (as explained here or here or in many other contributions). However, as it comes to XML-parsing, I couldn't find any information on whether this "Codable" protocol could also be used for XML-decoding. I tried to use the XMLParser (as can be seen in code-excerts below). But I failed to used the "Codable" protocol as to simplify the XML-parsing process. How would I have to use the Codable-protocol exactly to simplify XML-parsing ?? // the Fetching of the XML-data (excert shown here with a

Using Alamofire and Codable for put request

跟風遠走 提交于 2019-11-30 15:38:45
问题 I have a simple put request and I am using Alamofire's Parameters type to send data to the server. I would like to use codable. How do I either convert my codable struct to parameters or reconfigure the Alamofire request to take JSON objects as the parameter? What is the best and most efficient way to send post and put requests using Alamofire? Here is what I am doing right now with Alamofire. func addProduct(product:MainProduct, completionHandler:@escaping ((JSON?, Error?)->Void)) { let url

Why can I use Codable with a project language version of Swift 3.3?

拟墨画扇 提交于 2019-11-30 06:03:05
问题 Codable got introduce in Swift 4. My project language version is Swift 3.3 but still I am able to use Codable in my project. This is not the issue but how is it possible? I just want to know. 回答1: I think may be you are using latest Swift compiler. 回答2: The Swift compiler version and "Swift Language Version" build setting (corresponding to the -swift-version command-line flag) are two different things. The former is the actual version of compiler and standard library you're using, whereas the

Swift 4 JSON Codable - value returned is sometimes an object, others an array

扶醉桌前 提交于 2019-11-30 05:24:03
问题 the data I'm getting from an API returns a single object but when there's multiple objects, it returns an array in the same key. With the current model (struct) I'm working with, the decoding fails when an array shows up. These results are randomly ordered, meaning I can't know when I will be served an object or array. Is there a way to create a model that takes this fact into account and can assign the correct type to cast for the value ('String' or '[String]') so that the decoding continues

Codable and XMLParser in Swift4

怎甘沉沦 提交于 2019-11-30 02:40:32
问题 Using Swift4, iOS11.1, Xcode9.1, Using the new Swift4 typealiase "Codable" works well for JSON-decoding (as explained here or here or in many other contributions). However, as it comes to XML-parsing, I couldn't find any information on whether this "Codable" protocol could also be used for XML-decoding. I tried to use the XMLParser (as can be seen in code-excerts below). But I failed to used the "Codable" protocol as to simplify the XML-parsing process. How would I have to use the Codable

How to handle partially dynamic JSON with Swift Codable?

跟風遠走 提交于 2019-11-30 02:06:56
I've got some JSON messages coming in over a websocket connection. // sample message { type: "person", data: { name: "john" } } // some other message { type: "location", data: { x: 101, y: 56 } } How can I convert those messages into proper structs using Swift 4 and the Codable protocol? In Go I can do something like: "Hey at the moment I only care about the type field and I'm not interested in the rest (the data part)." It would look like this type Message struct { Type string `json:"type"` Data json.RawMessage `json:"data"` } As you can see Data is of type json.RawMessage which can be parsed

Init an object conforming to Codable with a dictionary/array

﹥>﹥吖頭↗ 提交于 2019-11-29 13:15:27
问题 Primarily my use case is to create an object using a dictionary: e.g. struct Person: Codable { let name: String } let dictionary = ["name": "Bob"] let person = Person(from: dictionary) I would like to avoid writing custom implementations and want to be as efficient as possible. 回答1: At the moment the best solution I have is this but it has the overhead of encoding/decoding. extension Decodable { init(from: Any) throws { let data = try JSONSerialization.data(withJSONObject: from, options:

How to use Codable protocol in objective c data model class?

前提是你 提交于 2019-11-29 10:25:01
I am working on mix and match iOS source code. I have implemented codable for swift data model class which reduces the burden of writing parser logic. I tried to conform my objective c class to codable protcol which in turn thrown an error "Cannot find protocol declaration for 'Codable'". Is there any way to use this swift protocol into objective c class? Or Is there any other objective c api that provides the same capability as Codable? The idea is to make the parsing logic same across swift and objective c classes. Yes, you can use Codable together with Obj-C. The tricky part is that because

How to conform UIImage to Codable?

六月ゝ 毕业季﹏ 提交于 2019-11-29 03:43:05
Swift 4 has Codable and it's awesome. But UIImage does not conform to it by default. How can we do that? I tried with singleValueContainer and unkeyedContainer extension UIImage: Codable { // 'required' initializer must be declared directly in class 'UIImage' (not in an extension) public required init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() let data = try container.decode(Data.self) guard let image = UIImage(data: data) else { throw MyError.decodingFailed } // A non-failable initializer cannot delegate to failable initializer 'init(data:)' written