RStorage 1.2.2

RStorage 1.2.2

Erik BasarginKirill SaltykovAlexey Voronov 维护。



RStorage 1.2.2

RStorage Build Status Version codebeat badge codecov

具有缓存的 UserDefaults 抽象框架

安装

CocoaPods

对于 RStorage,在 Podfile 中添加以下条目

pod 'RStorage', '~> 1.2'

然后运行 pod install

在任何想要使用 RStorage 的文件中,不要忘记使用 import RStorage 导入框架。

SwiftPM (Accio)

使用Accio(或Xcode 11的SwiftPM)安装RStorage,请在Package.swift文件中添加以下行

.package(url: "https://github.com/ephedra-software/RStorage.git", .upToNextMajor(from: "1.2.2"))

然后运行accio installaccio update

在任何想要使用 RStorage 的文件中,不要忘记使用 import RStorage 导入框架。

使用方法

使用RStorage非常简单。您可以通过以下方式访问API

let storage: RStorage = RStorage<KeyManager>.instance

struct YourCodableStructure: Codable {
    let name: String
}

do {
    try storage.save(key: KeyManager.keys.keyOne, value: URL("https://www.google.com/")!)
    try storage.save(key: KeyManager.keys.keyTwo, value: YourCodableStructure(name: "Struct"))
    
    let data1 = try storage.load(key: KeyManager.keys.keyOne)
    let data2 = try storage.load(key: KeyManager.keys.keyTwo)
    
    print("Url: \(data1 ?? "Not found url")")                   // Url: https://www.google.com/
    print("Data name: \(data2?.name ?? "Not found data2")")     // Data name: Struct
} catch {
    print(error.localizedDescription)
}

storage.removeAll(without: KeyManager.keyOne)

do {
    let data1 = try storage.load(key: KeyManager.keys.keyOne)
    let data2 = try storage.load(key: KeyManager.keys.keyTwo)
    
    print("Url: \(data1 ?? "Not found url")")                   // Url: https://www.google.com/
    print("Data name: \(data2?.name ?? "Not found data2")")     // Data name: Not found data2
} catch {
    print(error.localizedDescription)
}

要完成此操作,您必须实现以下

enum KeyManager: String, RStorageManagerProtocol {
    typealias SupportedKeys = (
        keyOne: Key<URL, KeyManager>,
        keyTwo: Key<YourCodableStructure, KeyManager>
    )
    
    case keyOne = "__DefaultType__"
    case keyTwo = "__YourCodableStructure__"
    
    static var keys: SupportedKeys {
        return (
            Key(.keyOne),
            Key(.keyTwo)
        )
    }

    var useCache: Bool {
        switch self {
        case .keyOne: return false
        case .keyTwo: return true
        }
    }

    var usePersistentStorage: Bool {
        switch self {
        case .keyOne: return true
        case .keyTwo: return true
        }
    }
    
    var name: String {
        return self.rawValue
    }
}

许可证

RStorage在MIT许可证下发布。有关更多信息,请参阅LICENSE