Skip to content

Instantly share code, notes, and snippets.

@qdequele
Created June 10, 2015 06:41
Show Gist options
  • Save qdequele/2fc9818a4517f344220b to your computer and use it in GitHub Desktop.
Save qdequele/2fc9818a4517f344220b to your computer and use it in GitHub Desktop.
How to create a good service for your NSUserDefault Data (Local Storage)
//
// 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