简体   繁体   中英

How to save a swift struct to file

I want to use structs for the (very simple) model of my app.

However NSKeyedArchiver only accepts objects (extending NSObjects ).

Is there any good way to save a struct to a file?

A very simple approach I used sometimes. The quantity of code you need to write is no more then in the class/NSCoding scenario.

First of all import the great SwiftyJSON lib.

Let's start with a simple struct

struct Starship {
    let name: String
    let warpSpeed: Bool
    let captain: String?

    init(name: String, warpSpeed: Bool, captain: String? = nil) {
        self.name = name
        self.warpSpeed = warpSpeed
        self.captain = captain
    }
}

Let's make it convertible to/from a JSON

struct Starship {
    let name: String
    let warpSpeed: Bool
    let captain: String?

    init(name: String, warpSpeed: Bool, captain: String? = nil) {
        self.name = name
        self.warpSpeed = warpSpeed
        self.captain = captain
    }

    init?(json: JSON) {
        guard let
            name = json["name"].string,
            warpSpeed = json["warpSpeed"].bool
        else { return nil }
        self.name = name
        self.warpSpeed = warpSpeed
        self.captain = json["captain"].string
    }

    var asJSON: JSON {
        var json: JSON = [:]
        json["name"].string = name
        json["warpSpeed"].bool = warpSpeed
        json["captain"].string = captain
        return json
    }
}

That's it. Let's use it

let enterprise = Starship(name: "Enteriprise D", warpSpeed: true, captain: "JeanLuc Picard")

let json = enterprise.asJSON
let data = try! json.rawData()

// save data to file and reload it

let newJson = JSON(data: data)
let ship = Starship(json: newJson)
ship?.name // "Enterprise D"

Updated solution for Swift 5+

  • No need to import anything
  • Make your struct codable
struct Starship: Codable { // Add this
    let name: String
    let warpSpeed: Bool
    let captain: String?

    init(name: String, warpSpeed: Bool, captain: String? = nil) {
        self.name = name
        self.warpSpeed = warpSpeed
        self.captain = captain
    }
}
  • Functions to read from and write to file
var data: Starship

func readFromFile(filePathURL: URL) throws {
    let readData = try Data(contentsOf: filePathURL)
    self.data = try JSONDecoder().decode(Starship.self, from: readData)
}

func writeToFile(filePathURL: URL) throws {
    let jsonData = try JSONEncoder().encode(self.data)
    try jsonData.write(to: filePathURL)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM