Swift 字典转模型(KakaJSON) ---2.数据类型

由于JSON格式能表达的数据类型是比较有限的,所以服务器返回的JSON数据有时无法自动转换成客户端想要的数据类型。

  • 比如服务器返回的时间可能是个毫秒数1565480696,但客户端想要的是Date类型
  • 再比如客户端想要的是浮点数Double类型,但服务器返回的可能是字符串类型"6.45"
  • 再比如客户端想要的是Bool类型,但服务器返回的可能是"true"、"1"、0
  • 再比如客户端想要的是高精度浮点数NSDecimalNumber类型,服务器为了维持精度可能返回字符串"0.1234567890123456789"

1. Int

struct Student: Convertible {
    var age1: Int8 = 6
    var age2: Int16 = 0
    var age3: Int32 = 0
    var age4: Int64 = 0
    var age5: UInt8 = 0
    var age6: UInt16 = 0
    var age7: UInt32 = 0
    var age8: UInt64 = 0
    var age9: UInt = 0
    var age10: Int = 0
    var age11: Int = 0
}

let json: [String: Any] = [
    "age1": "suan8fa8",
    "age2": "6suan8fa8",
    "age3": "6",
    "age4": 6.66,
    "age5": NSNumber(value: 6.66),
    "age6": Int32(6),
    "age7": true,
    "age8": "FALSE",
    "age9": Decimal(6.66),
    "age10": NSDecimalNumber(value: 6.66),
    "age11": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("age1:\(student.age1)  age2:\(student.age2)  age3:\(student.age3)  age4:\(student.age4)  age5:\(student.age5)  age6:\(student.age6)  age7:\(student.age7)  age8:\(student.age8)  age9:\(student.age9)  age10:\(student.age10)  age11:\(student.age11)")
/*
age1:6  age2:6  age3:6  age4:6  age5:6  age6:6  age7:1  age8:0  age9:6  age10:6  age11:1565922866
*/

2. Float

struct Student: Convertible {
    var height1: Float = 0.0
    var height2: Float = 0.0
    var height3: Float = 0.0
    var height4: Float = 0.0
    var height5: Float = 0.0
    var height6: Float = 0.0
    var height7: Float = 0.0
    var height8: Float = 0.0
    var height9: Float = 0.0
}

let json: [String: Any] = [
    "height1": "6.66suan8fa8",
    "height2": "0.12345678",
    "height3": NSDecimalNumber(string: "0.12345678"),
    "height4": Decimal(string: "0.12345678") as Any,
    "height5": 666,
    "height6": true,
    "height7": "NO",
    "height8": CGFloat(0.12345678),
    "height9": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("height1:\(student.height1)  height2:\(student.height2)  height3:\(student.height3)  height4:\(student.height4)  height5:\(student.height5)  height6:\(student.height6)  height7:\(student.height7)  height8:\(student.height8)  height9:\(student.height9)")
/*
height1:6.66  height2:0.12345678  height3:0.12345678  height4:0.12345678  height5:666.0  height6:1.0  height7:0.0  height8:0.12345678  height9:1.5659228e+09
*/

3. Double

struct Student: Convertible {
    var height1: Double = 0.0
    var height2: Double = 0.0
    var height3: Double = 0.0
    var height4: Double = 0.0
    var height5: Double = 0.0
    var height6: Double = 0.0
    var height7: Double = 0.0
    var height8: Double = 0.0
    var height9: Double = 0.0
}

let json: [String: Any] = [
    "height1": "6.66suan8fa8",
    "height2": "0.1234567890123456",
    "height3": NSDecimalNumber(string: "0.1234567890123456"),
    "height4": Decimal(string: "0.1234567890123456") as Any,
    "height5": 666,
    "height6": true,
    "height7": "NO",
    "height8": CGFloat(0.1234567890123456),
    "height9": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("height1:\(student.height1)  height2:\(student.height2)  height3:\(student.height3)  height4:\(student.height4)  height5:\(student.height5)  height6:\(student.height6)  height7:\(student.height7)  height8:\(student.height8)  height9:\(student.height9)")
/*
height1:6.66  height2:0.1234567890123456  height3:0.1234567890123456  height4:0.1234567890123456  height5:666.0  height6:1.0  height7:0.0  height8:0.1234567890123456  height9:1565922866.0
*/

4. CGFloat

struct Student: Convertible {
    var height1: CGFloat = 0.0
    var height2: CGFloat = 0.0
    var height3: CGFloat = 0.0
    var height4: CGFloat = 0.0
    var height5: CGFloat = 0.0
    var height6: CGFloat = 0.0
    var height7: CGFloat = 0.0
    var height8: CGFloat = 0.0
    var height9: CGFloat = 0.0
}

let json: [String: Any] = [
    "height1": "6.66suan8fa8",
    "height2": "0.1234567890123456",
    "height3": NSDecimalNumber(string: "0.1234567890123456"),
    "height4": Decimal(string: "0.1234567890123456") as Any,
    "height5": 666,
    "height6": true,
    "height7": "NO",
    "height8": 0.1234567890123456,
    "height9": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("height1:\(student.height1)  height2:\(student.height2)  height3:\(student.height3)  height4:\(student.height4)  height5:\(student.height5)  height6:\(student.height6)  height7:\(student.height7)  height8:\(student.height8)  height9:\(student.height9)")
/*
height1:6.66  height2:0.1234567890123456  height3:0.1234567890123456  height4:0.1234567890123456  height5:666.0  height6:1.0  height7:0.0  height8:0.1234567890123456  height9:1565922866.0
*/

5. Bool

struct Student: Convertible {
    var rich1: Bool = false
    var rich2: Bool = false
    var rich3: Bool = false
    var rich4: Bool = false
    var rich5: Bool = false
    var rich6: Bool = false
}

let json: [String: Any] = [
    "rich1": 100,
    "rich2": 0.0,
    "rich3": "1",
    "rich4": NSNumber(value: 0.666),
    "rich5": "true",
    "rich6": "NO"
]

// 数值0为false,数值非0都是true
let student = json.kj.model(Student.self)
print("rich1:\(student.rich1)  rich2:\(student.rich2)  rich3:\(student.rich3)  rich4:\(student.rich4)  rich5:\(student.rich5)  rich6:\(student.rich6)")
/*
rich1:true  rich2:false  rich3:true  rich4:true  rich5:true  rich6:false
*/

6. String

// 不管是String、NSString,还是NSMutableString,都是等效支持的
struct Student: Convertible {
    var name1: String = ""
    var name2: String = ""
    var name3: NSString = ""
    var name4: NSString = ""
    var name5: NSMutableString = ""
    var name6: NSMutableString = ""
    var name7: String = ""
    var name8: String = ""
    var name9: String = ""
}

let json: [String: Any] = [
    "name1": 666,
    "name2": NSMutableString(string: "777"),
    "name3": [1,[2,3],"4"],
    "name4": NSDecimalNumber(string: "0.123456789012345678901234567890123456789"),
    "name5": 6.66,
    "name6": false,
    "name7": NSURL(fileURLWithPath: "/users/mj/desktop"),
    "name8": URL(string: "http://www.520suanfa.com") as Any,
    "name9": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("name1:\(student.name1)  name2:\(student.name2)  name3:\(student.name3)  name4:\(student.name4)  name5:\(student.name5)  name6:\(student.name6)  name7:\(student.name7)  name8:\(student.name8)  name9:\(student.name9)")
/*
name1:666  name2:777  name3:[1, [2, 3], "4"]  name4:0.123456789012345678901234567890123456789  name5:6.66  name6:false  name7:file:///users/mj/desktop  name8:http://www.520suanfa.com  name9:1565922866
*/

7. Decimal

struct Student: Convertible {
    var money1: Decimal = 0
    var money2: Decimal = 0
    var money3: Decimal = 0
    var money4: Decimal = 0
    var money5: Decimal = 0
    var money6: Decimal = 0
    var money7: Decimal = 0
    var money8: Decimal = 0
}

let json: [String: Any] = [
    "money1": 0.1234567890123456,
    "money2": true,
    "money3": NSDecimalNumber(string: "0.123456789012345678901234567890123456789"),
    "money4": "0.123456789012345678901234567890123456789",
    "money5": 666,
    "money6": "NO",
    "money7": CGFloat(0.1234567890123456),
    "money8": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("money1:\(student.money1)  money2:\(student.money2)  money3:\(student.money3)  money4:\(student.money4)  money5:\(student.money5)  money6:\(student.money6)  money7:\(student.money7)  money8:\(student.money8)")
/*
money1:0.1234567890123456  money2:1  money3:0.123456789012345678901234567890123456789  money4:0.123456789012345678901234567890123456789  money5:666  money6:0  money7:0.1234567890123456  money8:1565922866
*/

8. NSDecimalNumber

struct Student: Convertible {
    var money1: NSDecimalNumber = 0
    var money2: NSDecimalNumber = 0
    var money3: NSDecimalNumber = 0
    var money4: NSDecimalNumber = 0
    var money5: NSDecimalNumber = 0
    var money6: NSDecimalNumber = 0
    var money7: NSDecimalNumber = 0
    var money8: NSDecimalNumber = 0
}

let json: [String: Any] = [
    "money1": 0.1234567890123456,
    "money2": true,
    "money3": Decimal(string: "0.123456789012345678901234567890123456789") as Any,
    "money4": "0.123456789012345678901234567890123456789",
    "money5": 666.0,
    "money6": "NO",
    "money7": CGFloat(0.1234567890123456),
    "money8": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("money1:\(student.money1)  money2:\(student.money2)  money3:\(student.money3)  money4:\(student.money4)  money5:\(student.money5)  money6:\(student.money6)  money7:\(student.money7)  money8:\(student.money8)")
/*
money1:0.1234567890123456  money2:1  money3:0.123456789012345678901234567890123456789  money4:0.123456789012345678901234567890123456789  money5:666  money6:0  money7:0.1234567890123456  money8:1565922866
*/

9. NSNumber

struct Student: Convertible {
    var money1: NSNumber = 0
    var money2: NSNumber = 0
    var money3: NSNumber = 0
    var money4: NSNumber = 0
    var money5: NSNumber = 0
    var money6: NSNumber = 0
    var money7: NSNumber = 0
    var money8: NSNumber = 0
}

let json: [String: Any] = [
    "money1": 0.1234567890123456,
    "money2": true,
    "money3": Decimal(string: "0.1234567890123456") as Any,
    "money4": "0.1234567890123456",
    "money5": 666.0,
    "money6": "NO",
    "money7": CGFloat(0.1234567890123456),
    "money8": Date(timeIntervalSince1970: 1565922866)
]

let student = json.kj.model(Student.self)
print("money1:\(student.money1)  money2:\(student.money2)  money3:\(student.money3)  money4:\(student.money4)  money5:\(student.money5)  money6:\(student.money6)  money7:\(student.money7)  money8:\(student.money8)")
/*
money1:0.1234567890123456  money2:1  money3:0.1234567890123456  money4:0.1234567890123456  money5:666  money6:0  money7:0.1234567890123456  money8:1565922866
*/

10. URL

// 支持URL和NSURL,默认会将String转为URL\NSURL

struct Student: Convertible {
    var url1: NSURL?
    var url2: NSURL?
    var url3: URL?
    var url4: URL?
}

let url = "http://520suanfa.com/红黑树"
let encodedUrl = "http://520suanfa.com/%E7%BA%A2%E9%BB%91%E6%A0%91"

let json: [String: Any] = [
    "url1": url,
    "url2": URL(string: encodedUrl) as Any,
    "url3": url,
    "url4": NSURL(string: encodedUrl) as Any
]

let student = json.kj.model(Student.self)
print("url1:\(student.url1!)  url2:\(student.url2!)  url3:\(student.url3!)  url4:\(student.url4!)")
/*
url1:http://520suanfa.com/%E7%BA%A2%E9%BB%91%E6%A0%91  url2:http://520suanfa.com/%E7%BA%A2%E9%BB%91%E6%A0%91  url3:http://520suanfa.com/%E7%BA%A2%E9%BB%91%E6%A0%91  url4:http://520suanfa.com/%E7%BA%A2%E9%BB%91%E6%A0%91
*/

11. Date

// 支持Date和NSDate,默认会将秒数转为Date\NSDate
struct Student: Convertible {
    var date1: NSDate?
    var date2: NSDate?
    var date3: Date?
    var date4: Date?
    var date5: Date?
    var date6: Date?
    var date7: Date?
}

let milliseconds: TimeInterval = 1565922866

let json: [String: Any] = [
    "date1": milliseconds,
    "date2": Date(timeIntervalSince1970: milliseconds),
    "date3": milliseconds,
    "date4": NSDate(timeIntervalSince1970: milliseconds),
    "date5": "\(milliseconds)",
    "date6": NSDecimalNumber(string: "\(milliseconds)"),
    "date7": Decimal(string: "\(milliseconds)") as Any
]

let student = json.kj.model(Student.self)
print("date1:\(student.date1!)  date2:\(student.date2!)  date3:\(student.date3!)  date4:\(student.date4!)  date5:\(student.date5!)  date6:\(student.date6!)  date7:\(student.date7!)")
/*
date1:2019-08-16 02:34:26 +0000  date2:2019-08-16 02:34:26 +0000  date3:2019-08-16 02:34:26 +0000  date4:2019-08-16 02:34:26 +0000  date5:2019-08-16 02:34:26 +0000  date6:2019-08-16 02:34:26 +0000  date7:2019-08-16 02:34:26 +0000
*/

12. Enum

// 遵守一下ConvertibleEnum协议即可支持带有RawValue的Enum类型

enum Grade: String, ConvertibleEnum {
    case perfect = "A"
    case great = "B"
    case good = "C"
    case bad = "D"
}

struct Student: Convertible {
    var grade1: Grade = .perfect
    var grade2: Grade = .perfect
}

let json: [String: Any] = [
    "grade1": "C",
    "grade2": "D"
]

let student = json.kj.model(Student.self)
print("grade1:\(student.grade1.rawValue)  grade2:\(student.grade2)")
// grade1:C  grade2:bad

/**********************************************/

// 换个浮点数试试
enum Grade2: Double, ConvertibleEnum {
    case perfect = 8.88
    case great = 7.77
    case good = 6.66
    case bad = 5.55
}

struct Student2: Convertible {
    var grade1: Grade2 = .perfect
    var grade2: Grade2 = .perfect
    var grade3: Grade2 = .perfect
    var grade4: Grade2 = .perfect
}

let json: [String: Any] = [
    "grade1": "5.55kaka",
    "grade2": 6.66,
    "grade3": NSNumber(value: 7.77),
    "grade4": NSDecimalNumber(string: "8.88")
]

let student = json.kj.model(Student2.self)
print("grade1:\(student.grade1)  grade2:\(student.grade2)  grade3:\(student.grade3)  grade4:\(student.grade4)")
// grade1:bad  grade2:good  grade3:great  grade4:perfect

13. Array

// 支持Array、NSArray、NSMutableArray与Set、NSSet、NSMutableSet之间自动转换

struct Person: Convertible {
    var array1: [Int]?
    var array2: NSArray?
    var array3: NSMutableArray?
    var array4: [Int]?
    var array5: NSArray?
    var array6: NSMutableArray?
}

let array = [1, 2, 3]

let json: [String: Any] = [
    "array1": NSMutableArray(array: array),
    "array2": array,
    "array3": array,
    "array4": NSMutableSet(array: array),
    "array5": NSSet(array: array),
    "array6": Set(array),
]

let person = json.kj.model(Person.self)
print("array1:\(person.array1!)  array2:\(person.array2!)  array3:\(person.array3!)  array4:\(person.array4!)  array5:\(person.array5!)  array6:\(person.array6!)")
/*
array1:[1, 2, 3]  array2:(
    1,
    2,
    3
)  array3:(
    1,
    2,
    3
)  array4:[3, 1, 2]  array5:(
    3,
    1,
    2
)  array6:(
    3,
    1,
    2
)
*/

14. Dictionary

// 支持Dictionary、NSDictionary、NSMutableDictionary之间自动转换

struct Person: Convertible {
    var dict1: [String: Any]?
    var dict2: NSDictionary?
    var dict3: NSMutableDictionary?
}

let dict = ["no1": 100, "no2": 200]

let json: [String: Any] = [
    "dict1": NSMutableDictionary(dictionary: dict),
    "dict2": dict,
    "dict3": dict
]

let person = json.kj.model(Person.self)
print("dict1:\(person.dict1!)  dict2:\(person.dict2!)  dict3:\(person.dict3!)")
/*
dict1:["no1": 100, "no2": 200]  dict2:{
    no1 = 100;
    no2 = 200;
}  dict3:{
    no1 = 100;
    no2 = 200;
}
*/

Swift 字典转模型(KakaJSON) ---1.基本用法
Swift 字典转模型(KakaJSON) ---3.key处理
Swift 字典转模型(KakaJSON) ---4.动态模型
Swift KakaJSON 05.---模型转字典
Swift KakaJSON 06.---Coding/归档/解档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容