【KakaJSON手册】07_Coding_归档_解档

KakaJSON可以只用一行代码将常用数据进行归档\解档

  • 后面代码中会用到 file 文件路径
// 文件路径(String或者URL都可以)
let file = "/Users/mj/Desktop/test.data"

String

let string1 = "123"
// 将字符串写入文件
write(string1, to: file)

// 从文件中读取字符串
let string2 = read(String.self, from: file)
XCTAssert(string2 == string1)

// 从文件中读取Int
XCTAssert(read(Int.self, from: file) == 123)

Date

let date1 = Date(timeIntervalSince1970: 1565922866)
// 将Date写入文件
write(date1, to: file)

// 从文件中读取Date
let date2 = read(Date.self, from: file)
XCTAssert(date2 == date1)

// 从文件中读取Int
XCTAssert(read(Int.self, from: file) == 1565922866)

Array

let array1 = ["Jack", "Rose"]
// 将数组写入文件
write(array1, to: file)

// 从文件中读取数组
let array2 = read([String].self, from: file)
XCTAssert(array2 == array1)

// 同样支持Set、Dictionary等

Model

struct Book: Convertible {
    var name: String = ""
    var price: Double = 0.0
}

struct Car: Convertible {
    var name: String = ""
    var price: Double = 0.0
}

struct Dog: Convertible {
    var name: String = ""
    var age: Int = 0
}

struct Person: Convertible {
    var name: String = "Jack"
    var car: Car? = Car(name: "Bently", price: 106.666)
    var books: [Book]? = [
        Book(name: "Fast C++", price: 666.6),
        Book(name: "Data Structure And Algorithm", price: 666.6),
    ]
    var dogs: [String: Dog]? = [
        "dog0": Dog(name: "Wang", age: 5),
        "dog1": Dog(name: "ErHa", age: 3),
    ]
}

// 将模型写入文件
write(Person(), to: file)

// 从文件中读取模型
let person = read(Person.self, from: file)

XCTAssert(person?.name == "Jack")
XCTAssert(person?.car?.name == "Bently")
XCTAssert(person?.car?.price == 106.666)
XCTAssert(person?.books?.count == 2)
XCTAssert(person?.dogs?.count == 2)

Model Array

struct Car: Convertible {
    var name: String = ""
    var price: Double = 0.0
}

let models1 = [
    Car(name: "BMW", price: 100.0),
    Car(name: "Audi", price: 70.0)
]
// 将模型数组写入文件
write(models1, to: file)

// 从文件中读取模型数组
let models2 = read([Car].self, from: file)
XCTAssert(models2?.count == models1.count)
XCTAssert(models2?[0].name == "BMW")
XCTAssert(models2?[0].price == 100.0)
XCTAssert(models2?[1].name == "Audi")
XCTAssert(models2?[1].price == 70.0)

Model Set

struct Car: Convertible, Hashable {
    var name: String = ""
    var price: Double = 0.0
}

let models1: Set<Car> = [
    Car(name: "BMW", price: 100.0),
    Car(name: "Audi", price: 70.0)
]

// 将模型Set写入文件
write(models1, to: file)

// 从文件中读取模型Set
let models2 = read(Set<Car>.self, from: file)!
XCTAssert(models2.count == models1.count)
for car in models2 {
    XCTAssert(["BMW", "Audi"].contains(car.name))
    XCTAssert([100.0, 70.0].contains(car.price))
}

Model Dictionary

struct Car: Convertible {
    var name: String = ""
    var price: Double = 0.0
}

let models1 = [
    "car0": Car(name: "BMW", price: 100.0),
    "car1": Car(name: "Audi", price: 70.0)
]

// 将模型字典写入文件
write(models1, to: file)

// 从文件中读取模型字典
let models2 = read([String: Car].self, from: file)
XCTAssert(models2?.count == models1.count)

let car0 = models2?["car0"]
XCTAssert(car0?.name == "BMW")
XCTAssert(car0?.price == 100.0)

let car1 = models2?["car1"]
XCTAssert(car1?.name == "Audi")
XCTAssert(car1?.price == 70.0)
posted @ 2019-08-22 16:38  M了个J  阅读(1643)  评论(0编辑  收藏  举报