第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

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

3 回答

?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

在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)")

? ? ? ? }

? ? }

查看完整回答
反對(duì) 回復(fù) 2019-11-05
?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


對(duì)于Swift 4


 var nsDictionary: NSDictionary?

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

    nsDictionary = NSDictionary(contentsOfFile: path)

 }

對(duì)于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中完美使用。我認(rèn)為他們可能希望很快將重點(diǎn)轉(zhuǎn)移到swift,但是目前swift API并沒有核心NSClasses的所有功能。


查看完整回答
反對(duì) 回復(fù) 2019-11-05
?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

如果我要將.plist轉(zhuǎn)換為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

}


查看完整回答
反對(duì) 回復(fù) 2019-11-05
  • 3 回答
  • 0 關(guān)注
  • 850 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)