3

i'm facing a problem with reading data from a plist file using swift my .plist file

my code:

        let Chapterpath = NSBundle.mainBundle().pathForResource("chapterMapping", ofType: "plist")
    let dict2 = NSDictionary(contentsOfFile: Chapterpath!)
    let chaptername = dict2?.objectForKey("chapterName")
    let chapterNumber = dict2?.objectForKey("pageNumber")

next i'm trying to add plist data to an array, should i simply use

var myArray = [chapterName]

my question: is the code above correct? or im i missing something and when i tried to print plist data using println((chapterName)) i got an error

thank you

Moshe
  • 55,729
  • 73
  • 263
  • 420
Tevfik Xung
  • 888
  • 2
  • 9
  • 18
  • How to ask a question: What's the problem? Did you try it? What did you try, exactly? When you did, what happened? Was it what you wanted? If not, what's the difference between what happened and what you wanted? – matt Dec 07 '14 at 17:45
  • post edited, thank you – Tevfik Xung Dec 07 '14 at 17:51

5 Answers5

2

First, your Root object in the plist is an NSArray not a NSDictionary.

Second, if you want to use KVC on Foundation Collections (I don't believe this works with Swift's Array) you need to call valueForKeyPath.

let chapterPath = NSBundle.mainBundle().pathForResource("chapterMapping", ofType: "plist")
if let arrayOfItems: [AnyObject] = NSArray(contentsOfFile: chapterPath!) {
    let chapterNames: [String] = arrayOfItems.valueForKeyPath("chapterName") as NSArray as [String]
    let pageNumbers: [Int] = arrayOfItems.valueForKeyPath("pageNumber") as NSArray as [Int]
}

Third, the swift-y way of doing this would be with the map function, but arrayOfItems would need to be a strongly-defined type and it might be more work than it's worth. Example:

let array: [ChapterMetaData] = // define it here
let chapterImages = array.map { $0.chapterImage }
Stephen Furlani
  • 6,586
  • 4
  • 27
  • 56
1

As you say you have an array with multiple element. objectForKey does not search the hall tree levels and gets you the first one with the name. you have multiple values a loop must be envolved. Try the following:

var Chapterpath:NSString = NSBundle.mainBundle().pathForResource("chapterMapping", ofType: "plist");
var chapters:NSArray = NSArray(contentsOfFile: Chapterpath);

for chapter in chapters {
    let chaptername = chapter["chapterName"]
    let chapterNumber = chapter["pageNumber"]
    println(chaptername);
}
hasan
  • 22,029
  • 10
  • 58
  • 94
  • var chapters:NSArray = NSArray(contentsOfFile: Chapterpath). i got an error: extra arguments 'contentsOfFile' in call – Tevfik Xung Dec 07 '14 at 18:19
1

Use following code to read Plist data to NSArray:

let path = NSBundle.mainBundle().pathForResource(plistName, ofType: "plist")
var list = NSArray(contentsOfFile: path!) as [[String:String]]
Karthikeyan Vaithilingam
  • 6,473
  • 10
  • 41
  • 60
Himanshu Mahajan
  • 4,458
  • 2
  • 28
  • 29
1

Simplest way to parse the AppConfig.plist file in project:

var dictionaryObj: NSDictionary?
if let filePath = NSBundle.mainBundle().pathForResource("AppConfig", ofType: "plist") 
{
    dictionaryObj = NSDictionary(contentsOfFile: filePath)
}
if let dict = dictionaryObj 
{
    //parse it as NSDictionary
}
else
{
    //dictionaryObj is nil or doesn't contain anything.
}
Rahul Raina
  • 2,883
  • 21
  • 27
0

You can use NSArray if Plist Root is Array:

let path = NSBundle.mainBundle().pathForResource("OrderHelper", ofType: "plist")
let myArray = NSArray(contentsOfFile: path!)

You can use NSDictionary if Plist Root is Dictionary:

let path = NSBundle.mainBundle().pathForResource("OrderHelper", ofType: "plist") {
let myDict = NSDictionary(contentsOfFile: path!)
Himanshu padia
  • 6,141
  • 1
  • 41
  • 44