2014-07-20 2 views
-1

안녕하세요,배열에 액세스하려면 어떻게해야합니까? SWIFT를 사용하여 plist 내에 객체가 있습니까?

여기를 둘러 보았지만 답변을 찾을 수 없습니다. SoundsList라는 plist가 있습니다. 루트 수준에서 사전 (기본값) 인 첫 번째 항목은 Sounds라는 배열이고 그 항목은 모두 문자열입니다.

배열 내의 항목에 액세스 할 수 있기를 원하지만이를 수행하는 방법을 찾지 못하는 것 같습니다.

let path = NSBundle.mainBundle().pathForResource("SoundsList", ofType: "plist") 
let dict = NSDictionary(contentsOfFile: path) 

위의 코드는 지금까지 나에게 경로와 사전 부분을 제공하는 발견 할 것입니다 - 나는 다른 포스트에서이 코드를 찾을 관리 - Swift - Read plist

감사를

데이브

답변

2

예 당신은 올바른

//Gives path of plist 
let path = NSBundle.mainBundle().pathForResource("SoundsList", ofType: "plist") 
//you are intializing dictionary from plist 
let dict = NSDictionary(contentsOfFile: path) 

이 코드는 당신이 그

{ 
"Sounds":(
      "sound1", 
      "sound2" 
     ) 

} 

처럼해야 dictinary의 구조를 설명한대로

을 사용할 수 있습니다 dictionary.You는 그래서 soundlist를 ACESS하기 위해 사전

println(dict) 

의 내용을 인쇄 할 수 있습니다 당신을 제공합니다

var soundsList = dict["Sounds"] 
    println(soundList) //this will print your soundList 

당신은

for item in soundsList { 
    println(item) //print sound1 and sound2 
} 
를 사용하여 각 개체를 반복 할 수

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

+0

위의 링크를 살펴본 후 Xcode에서 '추론'오류가 발생하면서이를 수정했습니다. var soundsList : Array = dict [ "Sounds"]를 배열로 사용 DJenkins

관련 문제