亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Swift中將plist作為字典?

如何在Swift中將plist作為字典?

慕哥9229398 2019-11-05 15:57:53
我在玩蘋果的新Swift編程語言,遇到了一些問題...當前,我正在嘗試讀取plist文件,在Objective-C中,我將執行以下操作以將內容作為NSDictionary獲取:NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];如何在Swift中將plist作為字典?我假設我可以通過以下方式獲得plist的路徑:let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist")當這可行時(如果正確的話?):如何將內容作為字典?還有一個更籠統的問題:是否可以使用默認的NS *類?我想是...還是我錯過了什么?據我所知,默認框架NS *類仍然有效并且可以使用嗎?
查看完整描述

3 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

在Swift 3.0中,從Plist讀取。


func readPropertyList() {

? ? ? ? var propertyListFormat =? PropertyListSerialization.PropertyListFormat.xml //Format of the Property List.

? ? ? ? var plistData: [String: AnyObject] = [:] //Our data

? ? ? ? let plistPath: String? = Bundle.main.path(forResource: "data", ofType: "plist")! //the path of the data

? ? ? ? let plistXML = FileManager.default.contents(atPath: plistPath!)!

? ? ? ? do {//convert the data to a dictionary and handle errors.

? ? ? ? ? ? plistData = try PropertyListSerialization.propertyList(from: plistXML, options: .mutableContainersAndLeaves, format: &propertyListFormat) as! [String:AnyObject]


? ? ? ? } catch {

? ? ? ? ? ? print("Error reading plist: \(error), format: \(propertyListFormat)")

? ? ? ? }

? ? }

查看完整回答
反對 回復 2019-11-05
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

您仍然可以在Swift中使用NSDictionaries:


對于Swift 4


 var nsDictionary: NSDictionary?

 if let path = Bundle.main.path(forResource: "Config", ofType: "plist") {

    nsDictionary = NSDictionary(contentsOfFile: path)

 }

對于Swift 3+


if let path = Bundle.main.path(forResource: "Config", ofType: "plist"),

   let myDict = NSDictionary(contentsOfFile: path){

    // Use your myDict here

}

和舊版的Swift


var myDict: NSDictionary?

if let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist") {

    myDict = NSDictionary(contentsOfFile: path)

}

if let dict = myDict {

    // Use your dict here

}

NSClasses仍然可用,并且可以在Swift中完美使用。我認為他們可能希望很快將重點轉移到swift,但是目前swift API并沒有核心NSClasses的所有功能。


查看完整回答
反對 回復 2019-11-05
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

如果我要將.plist轉換為Swift字典,這就是我要做的事情:


if let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist") {

  if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> {

    // use swift dictionary as normal

  }

}

為Swift 2.0編輯:


if let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist"), dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {

    // use swift dictionary as normal

}

為Swift 3.0編輯:


if let path = Bundle.main.path(forResource: "Config", ofType: "plist"), let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {

        // use swift dictionary as normal

}


查看完整回答
反對 回復 2019-11-05
  • 3 回答
  • 0 關注
  • 837 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號