Swift KakaJSON 06.---Coding/아카이빙/문서 해제

4374 단어
KakaJSON은 한 줄의 코드로 자주 사용하는 데이터를 압축 파일\해제할 수 있습니다
  • 다음 코드에는 file 파일 경로
  • 가 사용됩니다.
    //     (String  URL   )
    let file = "/Users/yfmac001/Desktop//test.json"
    

    1. String

    //     (String  URL   )
    let file = "/Users/yfmac001/Desktop//test.json"
    
    let string1 = "123"
    
    //         
    write(string1, to: file)
    
    //          
    let string2 = read(String.self, from: file)
    print(string2 ?? "")
    // 123
    

    2. Date

    //     (String  URL   )
    let file = "/Users/yfmac001/Desktop//test.json"
    
    let date1 = Date(timeIntervalSince1970: 1565922866)
    
    //         
    write(date1, to: file)
    
    //       Date
    let date2 = read(Date.self, from: file)
    print(date2 ?? Date())
    // 2019-08-16 02:34:26 +0000
    

    3. Array

    //     (String  URL   )
    let file = "/Users/yfmac001/Desktop//test.json"
    
    let array1 = ["Jack", "Rose"]
    //        
    write(array1, to: file)
    
    
    //         
    let array2 = read([String].self, from: file)
    print(array2 ?? Array())
    // ["Jack", "Rose"]
    

    4. Model

    //     (String  URL   )
    let file = "/Users/yfmac001/Desktop//test.json"
    
    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)
    print(person ?? Person())
    /*
    Person(name: "Jack", car: Optional(KKJson.ViewController.(unknown context at $1022e8f0c).(unknown context at $1022e8fc4).Car(name: "Bently", price: 106.666)), books: Optional([KKJson.ViewController.(unknown context at $1022e8f0c).(unknown context at $1022e8f54).Book(name: "Fast C++", price: 666.6), KKJson.ViewController.(unknown context at $1022e8f0c).(unknown context at $1022e8f54).Book(name: "Data Structure And Algorithm", price: 666.6)]), dogs: Optional(["dog1": KKJson.ViewController.(unknown context at $1022e8f0c).(unknown context at $1022e9024).Dog(name: "ErHa", age: 3), "dog0": KKJson.ViewController.(unknown context at $1022e8f0c).(unknown context at $1022e9024).Dog(name: "Wang", age: 5)]))
    */
    

    5.Model Array

    //     (String  URL   )
    let file = "/Users/yfmac001/Desktop//test.json"
    
    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)
    guard let models = models2 else {
       return
    }
    for car: Car in models {
       print(car)
    }
    /*
    Car(name: "BMW", price: 100.0)
    Car(name: "Audi", price: 70.0)
    */
    

    6.Model Dictionary

    //     (String  URL   )
    let file = "/Users/yfmac001/Desktop//test.json"
    
    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)
    guard let models = models2 else {
        return
    }
    for (key, value) in models {
        print("key:\(key)  value:\(value)")
    }
    /*
    key:car1  value:Car(name: "Audi", price: 70.0)
    key:car0  value:Car(name: "BMW", price: 100.0)
    */
    

    Swift 사전 회전 모형(KakaJSON)---1.기본용법 Swift 사전 회전 모형(KakaJSON)---2.데이터 유형 Swift 사전 변환 모델(KakaJSON)---3.키 처리 Swift 사전 회전 모형 (KakaJSON) ---4.동적 모형 Swift KakaJSON 05.---모델 전환 사전

    좋은 웹페이지 즐겨찾기