Created
June 10, 2015 06:41
-
-
Save qdequele/2fc9818a4517f344220b to your computer and use it in GitHub Desktop.
How to create a good service for your NSUserDefault Data (Local Storage)
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
// | |
// LocalStoreservices.swift | |
// | |
// Created by Quentin De Quelen on 03/06/15. | |
// | |
import UIKit | |
struct LocalStore { | |
//All Keys | |
private static let authTokenKey = "authTokenKey" | |
private static let userIdkey = "userIdkey" | |
private static let userDefaults = NSUserDefaults.standardUserDefaults() | |
//All setter | |
static func setAuthTokenKey(token: String) { | |
userDefaults.setObject(token, forKey: authTokenKey) | |
userDefaults.synchronize() | |
} | |
static func setUserIdkey(id: String) { | |
userDefaults.setObject(id, forKey: userIdkey) | |
userDefaults.synchronize() | |
} | |
//All getter | |
static func authToken() -> String? { | |
return userDefaults.stringForKey(authTokenKey) | |
} | |
static func userId() -> String? { | |
return userDefaults.stringForKey(userIdkey) | |
} | |
//All remover | |
static func removeAuthTokenKey() { | |
userDefaults.setObject("", forKey: authTokenKey) | |
userDefaults.synchronize() | |
} | |
static func removeUserIdkey() { | |
userDefaults.setObject("", forKey: userIdkey) | |
userDefaults.synchronize() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment