codable

Using Decodable in Swift 4 with Inheritance

此生再无相见时 提交于 2019-12-17 02:29:02
问题 Should the use of class inheritance break the Decodability of class. For example, the following code class Server : Codable { var id : Int? } class Development : Server { var name : String? var userId : Int? } var json = "{\"id\" : 1,\"name\" : \"Large Building Development\"}" let jsonDecoder = JSONDecoder() let item = try jsonDecoder.decode(Development.self, from:json.data(using: .utf8)!) as Development print(item.id ?? "id is nil") print(item.name ?? "name is nil") here output is: 1 name is

How do i get the response from Node.js server in iOS app?

扶醉桌前 提交于 2019-12-14 04:04:05
问题 I have a Node.js server that sends JWT(JSON Web Token) as response, when user logs in. How do I get the response in my Swift 4, iOS app? func handleLogin() { guard let username = usernameTextField.text, let password = passwordTextField.text else { print("Invalid form") return } guard let url = URL(string: "http://localhost:3000/users/authenticate") else { return } var request = URLRequest(url: url) request.httpMethod = "POST" request.addValue("application/json", forHTTPHeaderField: "Content

NSManagedObject and Codable for class stored in server and local storage

邮差的信 提交于 2019-12-13 20:36:15
问题 I am creating a class that represents data that is either in local storage or comes in from online. On runtime I will use a get request to get an array of these objects from a server and compare them to the existing ones in CoreData in order to determine a new set of objects to be used for the app and to be stored. As such I have created a CoreData managed class that represents my object as such it inherits NSManagedObject . This will allow me to deal with the local storage getting the array

WrapperOfNSCoding fails with single value

落爺英雄遲暮 提交于 2019-12-13 20:07:19
问题 A question from Implementing Codable for UIColor struct WrapperOfNSCoding<Wrapped>: Codable where Wrapped: NSCoding { var wrapped: Wrapped init(_ wrapped: Wrapped) { self.wrapped = wrapped } init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() let data = try container.decode(Data.self) guard let object = NSKeyedUnarchiver.unarchiveObject(with: data) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "failed to unarchive an object"

How do I parse this nested JSON using Codable with Swift?

为君一笑 提交于 2019-12-13 09:28:02
问题 I am trying to parse this JSON using Codable: { "users": [ { "id": 1, "name": "Allen Carslake", "userName": "acarslake0", "profileImage": "https://source.unsplash.com/random/400x400", "createdDate": "2019-07-08T00:00:00.000+0000" }, { "id": 2, "name": "Revkah Antuk", "userName": "rantuk1", "profileImage": "https://source.unsplash.com/random/400x400", "createdDate": "2019-07-05T00:00:00.000+0000" }, { "id": 3, "name": "Mirna Saffrin", "userName": "msaffrin2", "profileImage": "https://source

Using Codable to parse nested JSON data

吃可爱长大的小学妹 提交于 2019-12-13 09:00:46
问题 I'm trying to use Codable to parse JSON data. But having some problems when it comes down to an object with arrays. I've been trying to follow following answer, but i'm getting an error Type 'Feature' does not conform to protocol 'Encodable' The JSON data I want are the latitude and longitude data, but i'm struggling to hard to learn Codable . I can also add that I tried to grab the id and it worked fine, but when i'm trying to go deeper, it just gives me an error. Any advice? I do want to

parsing invalid JSON Swift 4

只谈情不闲聊 提交于 2019-12-13 08:47:41
问题 My Json looks like this: data = { "key":"value", "key":"value", "key":"value"} I've been trying to use Swift4 Codable protocols for parsing JSON and have used it to great effect but for the life of me I cannot break this structure. The outer data = makes it invalid JSON but I can't seem to find any way to modify the data before I attempt to pass it to the JSONDecoder. Is there any way I can just receive that data as a string so I can drop the outermost characters and just parse the remaining

How to decode a nested json key which holds dynamic object using codable

时间秒杀一切 提交于 2019-12-13 03:16:47
问题 I am getting some web response in the below format, The problem is testMap is of dynamic type. Key of the object as well as the data type is dynamic. testMap will be a dictionary type object, which'll contain dynamic data in its dynamic key. As you can see, it can be string, array of string or array of integers. {"results": [ {"tests": [{ "testsType": "A", "testMap": { "testId": "5a7c4fedec7fb72b7aa9b36e"}}] }, { "tests": [{ "testsType": "B", "testMap": { "myIds": ["2112"]}}] }] } I tried to

Swift: JSONEcoder EXC_BAD_ACCESS

半城伤御伤魂 提交于 2019-12-13 03:14:18
问题 I want to encode and decode some objects in my Swift app. I have the following 2 classes. enum TileSize : Int, Codable { case smallTile = 3 case mediumTile = 2 case largeTile = 1 } class Tile: NSObject, Codable { var tileSize: TileSize = .smallTile var imageName: String = ""; var isDragged = false private enum CodingKeys : String, CodingKey { case tileSize case imageName } } class ConsumptionTile: Tile { override var imageName: String { get { return "consumption" } set { super.imageName =

Decode PropertyList using Swift 4 Codable PropertyListDecoder()

微笑、不失礼 提交于 2019-12-12 13:29:21
问题 Im trying to decode a plist using PropertyListDecoder() but when I get to the trying to access the keys, I get an error that says it's in the wrong format. I'm at a loss on what I'm doing wrong. Im under the assumption I can decode a Plist file the same way I can decode a JSON file right? I don't know, I'm still new to this. //struct for PLists struct AccessControl: Decodable { enum AccessControlKeys: String, CodingKey { case api } enum KeySecretKeys: String, CodingKey { case apiKey = "KEY"