Created
October 28, 2017 13:53
-
-
Save jamesjmtaylor/3bf2c846a4c0a4700d1e0fa6a0389a7f to your computer and use it in GitHub Desktop.
Codable Bug?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
struct User: Codable { | |
var id: String? | |
var type: String? | |
var url: String? | |
var mobileNumber: String? | |
var email: String? | |
var firstName: String? | |
var lastName: String? | |
var profileImageURL: String? | |
var isOnline: Bool? | |
enum CodingKeys: String, CodingKey { | |
case id | |
case type | |
case url | |
case mobileNumber = "mobile_number" | |
case email | |
case firstName = "first_name" | |
case lastName = "last_name" | |
case profileImageURL = "profile_image_url" | |
case isOnline = "is_online" | |
} | |
} | |
struct Message: Codable { | |
var id: String? | |
var type: String? | |
var thread_id: String? | |
var threadType: String? | |
var sender: User? | |
var body: String? | |
var media: String? | |
var sentAt: Double? | |
var unread: Bool? | |
var status: String? | |
var url: String? | |
enum codingKeys: String, CodingKey { | |
case id | |
case type | |
case thread_id | |
case threadType = "thread_type" | |
case sender | |
case body | |
case media | |
case sentAt = "sent_at" | |
case unread | |
case status | |
case url | |
} | |
} | |
let json = | |
""" | |
{ | |
"id": "Jvbl6LY", | |
"type": "sms", | |
"thread_id": "60LrVL7", | |
"thread_type": "578a", | |
"delay_until": null, | |
"sender": { | |
"id": "EVkdNBx", | |
"type": "user", | |
"first_name": "Jerry", | |
"last_name": "Ward", | |
"mobile_number": "123-456-7890", | |
"profile_image_url": "", | |
"is_online": false, | |
"email": "jerryw+demo@jypsee.com" | |
}, | |
"body": "Here is a picture of our Coquille Suite. Let me know if you would like a reservation?", | |
"media": "", | |
"sent_at": 1509133604000.167, | |
"unread": false, | |
"status": "", | |
"url": "https://connect-staging.jypsee.com/api/sms/threads/60LrVL7/history/Jvbl6LY/" | |
} | |
""" | |
let decoder = JSONDecoder() | |
let data = json.data(using: .utf8)! | |
do { | |
//TODO: paginate as the user scrolls the tableView | |
let message = try decoder.decode(Message.self, from: data) | |
print(message.thread_id) | |
print(message.threadType) | |
print(message.sender?.firstName) | |
print(message.sender?.lastName) | |
} catch { | |
print(error) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment