使用此框架,您可以读取嵌入在正在运行的可执行文件内部的属性列表,以及存储在磁盘上的可执行文件的属性列表。这些类型的可执行文件通常是命令行工具。框架内置了对读取嵌入式 info 和 launchd 属性列表的支持。也可以指定自定义属性列表类型。

要查看使用此框架的可运行示例应用程序,请查看 SwiftAuthorizationSample

用法

属性列表以 Data 实例的形式返回。通常,您需要使用以下方法之一进行反序列化:

示例 — 读取内部属性列表,创建 Decodable

在可执行文件内部运行时,将 launchd 属性列表解码为自定义的 Decodable 结构体

struct LaunchdPropertyList: Decodable {
    let machServices: [String : Bool]
    let label: String
    
    private enum CodingKeys: String, CodingKey {
        case machServices = "MachServices"
        case label = "Label"
    }
}

let data = try EmbeddedPropertyListReader.launchd.readInternal()
let plist = try PropertyListDecoder().decode(LaunchdPropertyList.self, from: data)

示例 — 读取外部属性列表,创建 NSDictionary

对于外部可执行文件,将 info 属性列表反序列化为 NSDictionary

let executableURL = URL(fileUrlWithPath: <# path here #>)
let data = try EmbeddedPropertyListReader.info.readExternal(from: executableURL)
let plist = try PropertyListSerialization.propertyList(from: data,
                                                       options: .mutableContainersAndLeaves,
                                                       format: nil) as? NSDictionary

示例 — 使用 BundleVersion 创建 Decodable

解码 info 属性列表,使用 BundleVersion 来解码 CFBundleVersion 条目

struct InfoPropertyList: Decodable {
    let bundleVersion: BundleVersion
    let bundleIdentifier: String
    
    private enum CodingKeys: String, CodingKey {
        case bundleVersion = "CFBundleVersion"
        case bundleIdentifier = "CFBundleIdentifier"
    }
}

let data = try EmbeddedPropertyListReader.info.readInternal()
let plist = try PropertyListDecoder().decode(InfoPropertyList.self, from: data)