Created
October 7, 2017 23:18
-
-
Save hk0i/ba4b1cd38edd9e44d90959374a22b3d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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