codable

Not able to modify Codable Class Properties?

前提是你 提交于 2019-12-24 07:48:21
问题 New in Swift development. I have a class structure for my USER JSON. I am able to encode JSON value to User object but when I want to modify these properties changes not reflected in a class structure. Can you please help me. import Foundation struct UserInfo : Codable { let userId : String? var firstName : String? var lastName : String? let email : String? let phone : String? enum CodingKeys: String, CodingKey { case userId = "userId" case firstName = "firstName" case lastName = "lastName"

Not able to modify Codable Class Properties?

倖福魔咒の 提交于 2019-12-24 07:48:04
问题 New in Swift development. I have a class structure for my USER JSON. I am able to encode JSON value to User object but when I want to modify these properties changes not reflected in a class structure. Can you please help me. import Foundation struct UserInfo : Codable { let userId : String? var firstName : String? var lastName : String? let email : String? let phone : String? enum CodingKeys: String, CodingKey { case userId = "userId" case firstName = "firstName" case lastName = "lastName"

Swift 4 Decodable multiple containers

三世轮回 提交于 2019-12-24 07:07:35
问题 I'm trying to understand how could I parse this multiple container JSON to an object. I've tried this approach (Mark answer), but he explain how to solve it using one-level container. For some reason I can't mimic the behaviour for multiple containers. { "graphql": { "shortcode_media": { "id": "1657677004214306744", "shortcode": "BcBQHPchwe4" } } } class Post: Decodable { enum CodingKeys: String, CodingKey { case graphql // The top level "user" key case shortcode_media } enum PostKeys: String

Update a progress bar while JSONDecoder is decoding a huge object, in Swift

限于喜欢 提交于 2019-12-24 05:20:06
问题 I'm decoding several gigabytes of JSON encoded data like this let decoder = JSONDecoder() let table = try decoder.decode([LogRow].self, from: content!) where content is plain text. Now, this operation can take several minutes, depending on the size of content , and I'd like to show some kind of progress. This is a command line program, so even a periodic update on the length of table would be enough. The problem is that I don't see anything like a callback or something like that. I tried with

Using Codable to decode different classes using the same key

好久不见. 提交于 2019-12-23 06:05:10
问题 I'm working with an API that provides 2 JSON URLS. Each URL contains a nested container with different attributes that belong to the same class and object. JSON URL 1 { "last_updated": 1535936629, "xyz": 5, "data": { "dataList": [ { "id": "42", "a1": "a1value", "a2": "a2value", }, // ,,, ] } } JSON URL 2 { "last_updated": 1536639996, "xyz": 5, "data": { "dataList": [ { "id": "42", "a3": "a3value", "a4": "a4value", }, // ,,, ] } } I want to use these JSON URLS to create a single Codable

Mapping a dictionary to an array of structs in Swift

对着背影说爱祢 提交于 2019-12-23 04:48:29
问题 My app needs to represent an array of presets, where a preset is represented by the following struct: struct Preset: Codable { var name: String var value: Int } Using NSUserDefaultsController , NSTableView and CocoaBindings, I was able to create a Preferences window where I can add and remove presets, as well as edit them. They are persisted to the UserDefaults plist as follows: <plist version="1.0"> <dict> <key>presets</key> <array> <dict> <key>name</key> <string>A preset</string> <key>value

Swift codable, Default Value to Class property when key missing in the JSON

寵の児 提交于 2019-12-22 08:57:48
问题 As you know Codable is new stuff in swift 4, So we gonna move to this one from the older initialisation process for the Models. Usually we use the following Scenario class LoginModal { let cashierType: NSNumber let status: NSNumber init(_ json: JSON) { let keys = Constants.LoginModal() cashierType = json[keys.cashierType].number ?? 0 status = json[keys.status].number ?? 0 } } In the JSON cashierType Key may missing, so we giving the default Value as 0 Now while doing this with Codable is

Decoding an Array with Swift4 Codable

折月煮酒 提交于 2019-12-22 08:03:47
问题 I'm playing around with Swift 4 Codable again and just when I think I have the hang of it, I get this issue with decoding the Weather key from the response. let jsonData = """ {"coord":{"lon":-113,"lat":35}, "weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}], "base":"stations", "main":{"temp":291.15,"pressure":1022,"humidity":72,"temp_min":291.15,"temp_max":291.15}, "visibility":16093,"wind":{"speed":3.6,"deg":200}, "clouds":{"all":1}, "dt":1503294780,"sys":{"type":1

Swift Codable null handling

烈酒焚心 提交于 2019-12-21 04:22:20
问题 I have a struct that parse JSON using Codable . struct Student: Codable { let name: String? let amount: Double? let adress: String? } Now if the amount value is coming as null the JSON parsing is failing. So should I manually handle the null cases for all the Int and Double that are present in the Student struct? The String values coming as null is automatically handled. 回答1: Let me do this Playground for you since an example shows you more than a hundred words: import Cocoa struct Student:

Swift 4 codable: the keys are Int array in JSON data

天大地大妈咪最大 提交于 2019-12-20 05:44:21
问题 { "0": { "name": "legaldoc.pdf", "cmisid": "yib5C-w92PPtxTBlXl4UJ8oDBthDtAU9mKN5kh2_KrQ" }, "1": { "name": "persdoc.pdf", "cmisid": "dqAnrdNMXGTz1RbOMI37OY6tH9xMdxiTnz6wEl2m-VE" }, "2": { "name": "certdoc.pdf", "cmisid": "6d7DuhldQlnb0JSjXlZb9mMOjxV3E_ID-ynJ0QRPMOA" } } How do I use Swift 4 Codable to parse JSON data like that? the problem is that the keys are Int array. How do I set CodingKeys for this? 回答1: As mentioned in the comments there is no array. All collection types are