我无法从 plist 键值中解包一个值
I can't Unwrap a value from a plist key value
我是 swift 的新手,我正在尝试将 属性 从 nsDictionary 加载到 vTitle
var nsDictionary: NSDictionary?
if let path = Bundle.main.path(forResource: "AppData", ofType: "plist") {
nsDictionary = NSDictionary(contentsOfFile: path)
}
let vTitle:String = nsDictionary["LbVacationsTitle"]
当我调试时,我在 nsDictionary 中看到了正确的键,但我无法解开一个键的值
LbVacationsTitle 的类型是字符串
取决于您的风格偏好...
var nsDictionary: NSDictionary?
if let path = Bundle.main.path(forResource: "AppData", ofType: "plist") {
nsDictionary = NSDictionary(contentsOfFile: path)
}
if let dict = nsDictionary {
let vTitle = dict["LbVacationsTitle"] as? String
if let vt = vTitle {
// ...
}
}
...或者...
var nsDictionary: NSDictionary?
if let path = Bundle.main.path(forResource: "AppData", ofType: "plist") {
nsDictionary = NSDictionary(contentsOfFile: path)
}
guard let dict = nsDictionary else {
print("Couldn't get a valid dictionary")
return
}
let vTitle = dict["LbVacationsTitle"] as? String
guard let vt = vTitle else {
print("Couldn't find a string matching LbVacationsTitle")
return
}
// ...
请不要使用 Swift 中的 NSDictionary
API 来读取 属性 列表。
有PropertyListSerialization
(甚至PropertyListDecoder
)
let url = Bundle.main.url(forResource: "AppData", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let dictionary = try! PropertyListSerialization.propertyList(from: data, format: nil) as! [String:Any]
let vTitle = dictionary["LbVacationsTitle"] as! String
由于文件在应用程序包中是不可变的,任何崩溃都会揭示 设计 错误
我是 swift 的新手,我正在尝试将 属性 从 nsDictionary 加载到 vTitle
var nsDictionary: NSDictionary?
if let path = Bundle.main.path(forResource: "AppData", ofType: "plist") {
nsDictionary = NSDictionary(contentsOfFile: path)
}
let vTitle:String = nsDictionary["LbVacationsTitle"]
当我调试时,我在 nsDictionary 中看到了正确的键,但我无法解开一个键的值 LbVacationsTitle 的类型是字符串
取决于您的风格偏好...
var nsDictionary: NSDictionary?
if let path = Bundle.main.path(forResource: "AppData", ofType: "plist") {
nsDictionary = NSDictionary(contentsOfFile: path)
}
if let dict = nsDictionary {
let vTitle = dict["LbVacationsTitle"] as? String
if let vt = vTitle {
// ...
}
}
...或者...
var nsDictionary: NSDictionary?
if let path = Bundle.main.path(forResource: "AppData", ofType: "plist") {
nsDictionary = NSDictionary(contentsOfFile: path)
}
guard let dict = nsDictionary else {
print("Couldn't get a valid dictionary")
return
}
let vTitle = dict["LbVacationsTitle"] as? String
guard let vt = vTitle else {
print("Couldn't find a string matching LbVacationsTitle")
return
}
// ...
请不要使用 Swift 中的 NSDictionary
API 来读取 属性 列表。
有PropertyListSerialization
(甚至PropertyListDecoder
)
let url = Bundle.main.url(forResource: "AppData", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let dictionary = try! PropertyListSerialization.propertyList(from: data, format: nil) as! [String:Any]
let vTitle = dictionary["LbVacationsTitle"] as! String
由于文件在应用程序包中是不可变的,任何崩溃都会揭示 设计 错误