SimpleSession 0.2.2

SimpleSession 0.2.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016年11月
SwiftSwift版本3.0
SPM支持SPM

Nicolas Molina维护。



  • Nicolas Molina

索引

特性

  • 易于使用
  • 自定义会话协议
  • 默认会话协议使用用户默认设置

先决条件

  • iOS 8+
  • Xcode 7+
  • Swift 3.0

如何使用

查看示例项目以获取具体示例。

例子

// 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()

API

默认值

会话协议。默认使用用户默认设置

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文件。