FHPropertyWrappers

Xcode Build SwiftLint

一些有用的 Swift 属性包装器。

要求

安装

Swift Package Manager

将以下内容添加到您的 Package.swift 的依赖项中

.package(url: "https://github.com/FelixHerrmann/FHPropertyWrappers.git", from: "x.x.x")

手动

下载 Sources 文件夹中的文件并将它们拖到您的项目中。

Stored

一个属性包装器,用于在 UserDefaults 存储中读取和写入包装的值。

它支持 UserDefaults 允许的所有类型。 在此处查看所有支持的类型。

@Stored("string") var string = ""
@Stored("int") var int: Int
@Stored("array") var array: [String]
@Stored("dictionary") var dictionary [String: Int]

如果没有设置任何值,则默认值基于 defaultStoredValue

除此之外,也支持 OptionalRawRepresentableCodable。 对于非 RawRepresentable 枚举,请使用 Codable

@Stored("optional") var optional: String?
enum Enumeration: String, Storable {
    case firstCase
    case secondCase
    
    static var defaultStoredValue: Enumeration {
        return .firstCase
    }
}

@Stored("enumeration") var enumeration: Enumeration
struct CustomType: Codable, Storable {
    let name: String
    
    static var defaultStoredValue: CustomType {
        return CustomType(name: "")
    }
}

@Stored("codable") var codable: CustomType

包装的值必须符合 Storable

提示:要存储 Storable 类型的数组(不支持,仅允许 RawStorable 类型),请创建一个围绕数组的容器结构体,使其符合 Codable

SecureStored

一个属性包装器,用于在 Keychain 中读取和写入包装的值。

它支持所有基本类型,其中大多数依赖于 Codable。 在此处查看所有支持的类型。

@SecureStored("string") var string = ""
@SecureStored("int") var int: Int
@SecureStored("array") var array: [String]
@SecureStored("dictionary") var dictionary [String: Int]

如果没有设置任何值,则默认值基于 defaultStoredValue

除此之外,也支持 OptionalRawRepresentableCodable。 对于非 RawRepresentable 枚举,请使用 Codable

@SecureStored("optional") var optional: String?
enum Enumeration: String, SecureStorable {
    case firstCase
    case secondCase
    
    static var defaultStoredValue: Enumeration {
        return .firstCase
    }
}

@SecureStored("enumeration") var enumeration: Enumeration
struct CustomType: Codable, SecureStorable {
    let name: String
    
    static var defaultStoredValue: CustomType {
        return CustomType(name: "")
    }
}

@SecureStored("codable") var codable: CustomType

包装的值必须符合 SecureStorable

许可

FHPropertyWrappers 在 MIT 许可下可用。 有关更多信息,请参阅LICENSE文件。