我正在使用Swift构建命令行工具。我想将用户配置存储到plist中。
我发现我可以使用Copy Files Phase
下的Build Phase
复制plist,以便我可以在运行时访问它,如:
guard let tempPath = Bundle.main.path(forResource: "config", ofType:"plist") else {
printError("Cannot locate config file")
exit(-1)
}
self.configPath = tempPath
self.configDict = NSDictionary(contentsOfFile: self.configPath) as! [String:String]
获取用户信息后,我还可以通过以下方式成功更新该plist:
do {
let pData = try PropertyListSerialization.data(
fromPropertyList: self.configDict,
format: .xml,
options: 0)
try pData.write(to: URL(fileURLWithPath: self.configPath))
} catch {
printError("Failed to write dictionary to property list")
return
}
但是,在该过程完成后,原始plist根本不会更改。所以我猜Xcode的作用是将plist复制到产品目录中,然后在完成后删除该副本?
如何在Swift命令行工具中将用户配置写入plist?