Swift 读取 plist 并处理错误
Swift Reading plist and handle errors
我正在尝试创建一个函数,它将 return 一个包含我的 plist 路径的字符串并处理一些错误,例如 fileDoesntExist、notPlistFile、invalidConfiguration。
plist 在启动时作为参数被调用
--配置“${PROJECT_DIR}/configuration.plist”
我创建了一个带有错误的枚举:
enum PathError: Error {
case fileDoesntExist, notPlistFile, invalidConfiguration
}
我目前的功能是这样的:
func getConfigurationFilePath() throws -> String {
CommandLine.arguments
if let indexPath = CommandLine.arguments.firstIndex(where: {[=12=] == "--configuration"}) {
let url = URL(fileURLWithPath: CommandLine.arguments[indexPath + 1])
let data = try! Data(contentsOf: url)
let pListObject = try PropertyListSerialization.propertyList(from: data, options:PropertyListSerialization.ReadOptions(), format:nil)
let pListDict = pListObject as? Dictionary<String, AnyObject>
}
我的plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>OutputFile</key>
<string>/tmp/assessment_output.txt</string>
<key>ErrorFile</key>
<string>/tmp/assessment_error.txt</string>
<key>RunConfiguration</key>
<dict>
<key>RunInterval</key>
<integer>30</integer>
<key>Iterations</key>
<string>3</string>
</dict>
</dict>
</plist>
现在我很难弄清楚如何将这些错误插入到函数中。
任何 tips/suggestions?
如果你想抛出自定义错误而不是真正的错误你必须guard
所有可能失败的行
enum PathError: Error {
case invalidParameter, fileDoesntExist, notPlistFile, invalidConfiguration
}
func getConfigurationFilePath() throws -> String {
let args = CommandLine.arguments
guard let indexPath = args.firstIndex(where: {[=10=] == "--configuration"}),
indexPath + 1 < args.count else {
throw PathError.invalidParameter
}
let url = URL(fileURLWithPath: args[indexPath + 1])
guard let data = try? Data(contentsOf: url) else {
throw PathError.fileDoesntExist
}
guard let pListObject = try? PropertyListSerialization.propertyList(from: data, format: nil) else {
throw PathError.notPlistFile
}
guard let _ = pListObject as? Dictionary<String, Any> else {
throw PathError.invalidConfiguration
}
return url.path
}
我正在尝试创建一个函数,它将 return 一个包含我的 plist 路径的字符串并处理一些错误,例如 fileDoesntExist、notPlistFile、invalidConfiguration。 plist 在启动时作为参数被调用
--配置“${PROJECT_DIR}/configuration.plist”
我创建了一个带有错误的枚举:
enum PathError: Error {
case fileDoesntExist, notPlistFile, invalidConfiguration
}
我目前的功能是这样的:
func getConfigurationFilePath() throws -> String {
CommandLine.arguments
if let indexPath = CommandLine.arguments.firstIndex(where: {[=12=] == "--configuration"}) {
let url = URL(fileURLWithPath: CommandLine.arguments[indexPath + 1])
let data = try! Data(contentsOf: url)
let pListObject = try PropertyListSerialization.propertyList(from: data, options:PropertyListSerialization.ReadOptions(), format:nil)
let pListDict = pListObject as? Dictionary<String, AnyObject>
}
我的plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>OutputFile</key>
<string>/tmp/assessment_output.txt</string>
<key>ErrorFile</key>
<string>/tmp/assessment_error.txt</string>
<key>RunConfiguration</key>
<dict>
<key>RunInterval</key>
<integer>30</integer>
<key>Iterations</key>
<string>3</string>
</dict>
</dict>
</plist>
现在我很难弄清楚如何将这些错误插入到函数中。 任何 tips/suggestions?
如果你想抛出自定义错误而不是真正的错误你必须guard
所有可能失败的行
enum PathError: Error {
case invalidParameter, fileDoesntExist, notPlistFile, invalidConfiguration
}
func getConfigurationFilePath() throws -> String {
let args = CommandLine.arguments
guard let indexPath = args.firstIndex(where: {[=10=] == "--configuration"}),
indexPath + 1 < args.count else {
throw PathError.invalidParameter
}
let url = URL(fileURLWithPath: args[indexPath + 1])
guard let data = try? Data(contentsOf: url) else {
throw PathError.fileDoesntExist
}
guard let pListObject = try? PropertyListSerialization.propertyList(from: data, format: nil) else {
throw PathError.notPlistFile
}
guard let _ = pListObject as? Dictionary<String, Any> else {
throw PathError.invalidConfiguration
}
return url.path
}