如何在Swift中不使用NSDictionary读取Plist?

我已经在Swift 2中使用过这种方法

var myDict: NSDictionary? if let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist") { myDict = NSDictionary(contentsOfFile: path) } 

但是不知道如何在不使用NSDictionary的情况下读取Swift3中的plist (contentsOfFile:path)

原生的Swift方式是使用PropertyListSerialization

 if let url = Bundle.main.url(forResource:"Config", withExtension: "plist") { do { let data = try Data(contentsOf:url) let swiftDictionary = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String:Any] // do something with the dictionary } catch { print(error) } } 

您还可以使用NSDictionary(contentsOf:使用类型转换:

 if let url = Bundle.main.url(forResource:"Config", withExtension: "plist"), let myDict = NSDictionary(contentsOf: url) as? [String:Any] { print(myDict) } 

但你明确写道: 不使用NSDictionary(contentsOf …

基本上不使用NSDictionary而不在Swift NSDictionary进行转换,你就丢掉了重要的类型信息。