Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
UserDefault などに class を保存するサンプル
import Foundation
import ObjectMapper
public class User: NSObject, Mappable, NSCoding {
public var id: Int!
public var name: String!
public required init?(map: Map) {
public func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
required public init(coder: NSCoder) {
id = coder.decodeObject(forKey: "id") as! Int
name = coder.decodeObject(forKey: "name") as! String
public func encode(with aCoder: NSCoder) {
aCoder.encode(id, forKey: "id")
aCoder.encode(name, forKey: "name")
let user = User() = 0 = "name"
let data = NSKeyedArchiver.archivedData(withRootObject: user)
let unarchivedUser = NSKeyedUnarchiver.unarchiveObject(with: data) as! User
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment