Ios 读取Info.plist时强制转换失败
我正在尝试访问存储在Ios 读取Info.plist时强制转换失败,ios,swift,xcode,Ios,Swift,Xcode,我正在尝试访问存储在Info.plist中的值,第二行root=NSDictionary…失败 但是,我可以这样做来打开我的另一个文件,Common.plist if let path = Bundle.main.path(forResource: "Info", ofType: "plist"), let root = NSDictionary(contentsOfFile: path) as? [String: String] { 为什么我在打开Info.plist时遇到问题?请查看
Info.plist
中的值,第二行root=NSDictionary…
失败
但是,我可以这样做来打开我的另一个文件,Common.plist
if let path = Bundle.main.path(forResource: "Info", ofType: "plist"),
let root = NSDictionary(contentsOfFile: path) as? [String: String] {
为什么我在打开
Info.plist
时遇到问题?请查看您的Info.plist
。里面储存着各种各样的东西——不仅仅是字符串
。如果更改为则将root=NSDictionary(contentsOfFile:path)设置为?[字符串:任意]
您将开始通过该检查。查看您的Info.plist
。里面储存着各种各样的东西——不仅仅是字符串
。如果更改为则将root=NSDictionary(contentsOfFile:path)设置为?[String:Any]
您将开始通过该检查。您的Info.plist文件的内容不仅是String:String值,还包括String:Number等。因此String:AnyObject应该可以工作。您的Info.plist文件的内容不仅是String:String值,还包括String:Number,所以字符串:任何对象都应该工作。Info.plist
不是[String:String]
。它可以包括任何符合属性列表的类型
但是有一条非常方便的捷径
let root = Bundle.main.infoDictionary
返回
[String:Any]
信息。plist
不是[String:String]
。它可以包括任何符合属性列表的类型
但是有一条非常方便的捷径
let root = Bundle.main.infoDictionary
返回
[String:Any]
您可以这样获取:
func fetchData(){
if let path = Bundle.main.path(forResource: "YourPlistName", ofType: "plist") {
if let dicObj = NSDictionary(contentsOfFile: path) as? [String: Any] {
}
}
}
你可以这样取:
func fetchData(){
if let path = Bundle.main.path(forResource: "YourPlistName", ofType: "plist") {
if let dicObj = NSDictionary(contentsOfFile: path) as? [String: Any] {
}
}
}
不要再使用
AnyObject
。不要再使用AnyObject
。