测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2016年11月 |
SwiftSwift版本 | 3.0 |
SPM支持SPM | ✗ |
由Nicolas Molina维护。
查看示例项目以获取具体示例。
// String
SimpleSession.put("string", value: "This is a string")
print("Key: string\nValue: \(SimpleSession.get("string"))")
// Number
SimpleSession.put("number", value: 20.3)
print("Key: number\nValue: \(SimpleSession.get("number"))")
// JSON
SimpleSession.put("json", value: ["key": "value"])
print("Key: json\nValue: \(SimpleSession.get("json"))")
// NSData
SimpleSession.put("data", value: NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2))
print("Key: data\nValue: \(SimpleSession.get("data"))")
print("------------------------------")
// Remove an object from the session
SimpleSession.remove("string")
print("Remove Key: string\nValue: \(SimpleSession.get("string"))")
print("------------------------------")
// Clean the session
SimpleSession.clear()
会话协议。默认使用用户默认设置
SimpleSession.SESSION_PROTOCOL: SimpleSessionProtocol = UserDefaultsSession.sharedInstance // Default session use user defaults
// Configure other session
SimpleSession.SESSION_PROTOCOL = MySession()
SimpleSession.get(key: String, defaultValue: Any? = nil)
在会话中返回key
的值。如果没有在会话中找到key
,则返回defaultValue
。
SimpleSession.get("exist.key") // return value for "exist.key"
SimpleSession.get("not.exist.key") // return nil, because "not.exist.key" not exist in session
SimpleSession.get("not.exist.key2", defaultValue: 10) // return 10, because "not.exist.key" not exist in session, but defaultValue is set
SimpleSession.put(key: String, value: Any?)
在会话中将value
设置为key
。有效期在seconds
秒后。
SimpleSession.put("a.key", value: 10)
SimpleSession.has(key: String)
如果会话中有key
的值,则返回true
,否则返回false
。
SimpleSession.has("exist.key") // return true
SimpleSession.has("not.exist.key") // return false
SimpleSession.remove(key: String)
如果存在,则在会话中返回key
的值,并从会话中删除此键。
SimpleSession.remove("exist.key") // return value for "exist.key"
SimpleSession.remove("not.exist.key") // return nil, because "not.exist.key" not exist in session
移除会话中的所有键。
SimpleSession.clear()
import SimpleSession
open class MySession: SimpleSessionProtocol
{
fileprivate var session: [String : Any] = [:]
open func get(_ key: String, defaultValue: Any? = nil) -> Any? {
if let value = session[key] {
return value
}
return defaultValue
}
open func put(_ key: String, value: Any?) {
session[key] = value
}
open func has(_ key: String) -> Bool {
return get(key) != nil
}
@discardableResult open func remove(_ key: String) -> Any? {
return session.removeValueForKey(key)
}
}
// Configure in AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Configure session
SimpleSession.SESSION_PROTOCOL = MySession()
return true
}
// ...
}
SimpleSession可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。