[KakaJSON 매뉴얼] 07Coding_아카이빙문서를 해제하다

3896 단어
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(name: "BMW", price: 100.0),
        Car(name: "Audi", price: 70.0)
    ]
    
    //    Set    
    write(models1, to: file)
    
    //         Set
    let models2 = read(Set.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)

    좋은 웹페이지 즐겨찾기