简体   繁体   English

如何从Swift中的PLIST读取数据?

[英]How can I read data from a PLIST in Swift?

i'm facing a problem with reading data from a plist file using swift 我正在使用swift从plist文件中读取数据时遇到问题 我的.plist文件

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 接下来我试图将plist数据添加到数组中,我应该简单地使用

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 或者我错过了什么,当我尝试使用println((chapterName))打印plist数据时,我收到了一个错误

thank you 谢谢

First, your Root object in the plist is an NSArray not a NSDictionary . 首先,plist中的Root对象是NSArray而不是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 . 其次,如果你想在Foundation Collections上使用KVC(我不相信这适用于Swift的数组),你需要调用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. 第三,快速执行此操作的方法是使用map函数,但是arrayOfItems需要是一个强定义的类型,它可能比它的价值更多。 Example: 例:

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

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. objectForKey不搜索大厅树级别,并为您提供名称的第一个级别。 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);
}

Use following code to read Plist data to NSArray : 使用以下代码将Plist数据读取到NSArray

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

Simplest way to parse the AppConfig.plist file in project: 解析项目中AppConfig.plist文件的最简单方法:

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

You can use NSArray if Plist Root is Array: 如果Plist Root是数组,则可以使用NSArray:

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

You can use NSDictionary if Plist Root is Dictionary: 如果Plist Root是Dictionary,您可以使用NSDictionary:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM