How to read data from PLIST in Swift?

I have a problem 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 am trying to add plist data to an array, should I just use

var myArray = [chapterName]

      

my question is, is the code correct? or are they missing something and when I tried to print the plist data using println ((chapterName)) I got the error

Thank you

+3


source to share


5 answers


First, your object Root

in plist is NSArray

not a NSDictionary

.

Second, if you want to use KVC on Foundation collections (I don't think this works with Swift 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 quickest way to do this would be with a function map

, but it arrayOfItems

has to be a well-defined type, and that might be more work than it's worth. Example:

let array: [ChapterMetaData] = // define it here
let chapterImages = array.map { $0.chapterImage }

      

+2


source


As you say, you have an array with multiple elements. objectForKey does not look for levels of the hall tree and gets you first with a name. you have multiple values, the loop must be on. 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);
}

      

+1


source


Use the following code to read data Plist

into NSArray

:

let path = NSBundle.mainBundle().pathForResource(plistName, ofType: "plist")
var list = NSArray(contentsOfFile: path!) as [[String:String]]

      

+1


source


The easiest way to parse the AppConfig.plist file in a project is:

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.
}

      

+1


source


You can use NSArray if it Plist

Root

is an array:

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

      

You can use NSDictionary if Plist

Root

- Dictionary:

let path = NSBundle.mainBundle().pathForResource("OrderHelper", ofType: "plist") {
let myDict = NSDictionary(contentsOfFile: path!)

      

0


source







All Articles