从plist中检索数据

时间:2013-03-23 04:16:52

标签: ios xcode nsarray plist nsdictionary

我有一个plist,里面有一个数组,然后是一组字典元素?如何从plist中检索数据到我的数组?

plist

如何在一个数组中获取类别名称?

4 个答案:

答案 0 :(得分:31)

<强>目标C

// Read plist from bundle and get Root Dictionary out of it
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];

// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:@"catlist"]];

// Now a loop through Array to fetch single Item from catList which is Dictionary
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
    // Fetch Single Item
    // Here obj will return a dictionary
    NSLog(@"Category name : %@",[obj valueForKey:@"category_name"]);
    NSLog(@"Category id   : %@",[obj valueForKey:@"cid"]);
}];

<强>夫特

// Read plist from bundle and get Root Dictionary out of it
var dictRoot: [NSObject : AnyObject] = [NSObject : AnyObject].dictionaryWithContentsOfFile(NSBundle.mainBundle().pathForResource("data", ofType: "plist"))
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
var arrayList: [AnyObject] = [AnyObject].arrayWithArray((dictRoot["catlist"] as! String))
// Now a loop through Array to fetch single Item from catList which is Dictionary
arrayList.enumerateObjectsUsingBlock({(obj: AnyObject, index: UInt, stop: Bool) -> Void in
    // Fetch Single Item
    // Here obj will return a dictionary
    NSLog("Category name : %@", obj["category_name"])
    NSLog("Category id   : %@", obj["cid"])
})

Swift 2.0 Code

var myDict: NSDictionary?
    if let path = NSBundle.mainBundle().pathForResource("data", ofType: "plist") {
        myDict = NSDictionary(contentsOfFile: path)
    }
    let arrayList:Array = myDict?.valueForKey("catlist") as! Array<NSDictionary>
    print(arrayList)

    // Enumerating through the list
    for item in arrayList  {
        print(item)

    }

Swift 3.0

// Read plist from bundle and get Root Dictionary out of it
var dictRoot: NSDictionary?
if let path = Bundle.main.path(forResource: "data", ofType: "plist") {
    dictRoot = NSDictionary(contentsOfFile: path)
}

if let dict = dictRoot
{
    // Your dictionary contains an array of dictionary
    // Now pull an Array out of it.
    var arrayList:[NSDictionary] = dictRoot?["catlist"] as! Array
    // Now a loop through Array to fetch single Item from catList which is Dictionary
    arrayList.forEach({ (dict) in
        print("Category Name \(dict["category_name"]!)")
        print("Category Id \(dict["cid"])")
    })
}

答案 1 :(得分:4)

  1. 从捆绑包或任何目录中获取文件路径
  2. 从plist
  3. 中检索字典中获取数组
  4. 获取存储在数组中的字典

    NSString *plistFilePath  = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test.plist"];
    
    NSDictionary *list = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
    NSLog(@"%@",list);
    NSArray      *data = [list objectForKey:@"catlist"];
    for(int i=0; i< [data count]; i++)
    {
        NSMutableDictionary *details=[data objectAtIndex:i];
        NSLog(@"%@",[details objectForKey:@"category_name"]);
        NSLog(@"%@",[details objectForKey:@"cid"]);
    
    }
    

答案 2 :(得分:0)

PropertyListDecoder 可用于将plist文件直接解码为对象。 有关详细信息,请参见此答案https://stackoverflow.com/a/60389142/5662893

答案 3 :(得分:-1)

目标=&gt;中添加.plist文件复制捆绑资源 以上回复