Skip to content

Instantly share code, notes, and snippets.

@hk0i
Created October 7, 2017 23:18
Show Gist options
  • Save hk0i/ba4b1cd38edd9e44d90959374a22b3d2 to your computer and use it in GitHub Desktop.
Save hk0i/ba4b1cd38edd9e44d90959374a22b3d2 to your computer and use it in GitHub Desktop.
@objc(User)
final class User: NSObject, NSCoding {
let userId: Int
var email: String?
let name: String
var recentGames: [Int]?
/// nscoding keys
private static let ID_KEY = "id"
private static let EMAIL_KEY = "email"
private static let NAME_KEY = "name"
private static let RECENT_GAMES_KEY = "recentGames"
init(userId: Int, email: String?, name: String, recentGames: [Int]?) {
self.userId = userId
self.email = email
self.name = name
self.recentGames = recentGames
}
required convenience init?(coder aDecoder: NSCoder) {
let userId = aDecoder.decodeInteger(forKey: User.ID_KEY)
let email = aDecoder.decodeObject(forKey: User.EMAIL_KEY) as? String ?? nil
let name = aDecoder.decodeObject(forKey: User.NAME_KEY) as? String ?? ""
let recentGames = aDecoder.decodeObject(forKey: User.RECENT_GAMES_KEY) as? [Int] ?? nil
self.init(userId: userId, email: email, name: name, recentGames: recentGames)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(userId, forKey: User.ID_KEY)
aCoder.encode(email, forKey: User.EMAIL_KEY)
aCoder.encode(name, forKey: User.NAME_KEY)
aCoder.encode(recentGames, forKey: User.RECENT_GAMES_KEY)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment