7

I have a plist and inside that an array and then set of dictionary elements? How can I retrieve data from the plist to my array?

plist

How can I get category names in one array?

Chris Nolet
  • 8,714
  • 7
  • 67
  • 92
Naveen
  • 1,251
  • 9
  • 20
  • 38
  • 1
    Why do you need to make an array of category_name? This is a well structured data. If you want to access it easily, try making a model class for category with properties categoryName and categoryID. This would be easier. – Anupdas Mar 23 '13 at 04:42

4 Answers4

31

Objective-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"]);
}];

Swift

// 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"])")
    })
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
5
  1. Get your file path from bundle or from any directory
  2. Get the array from dictionary retrieved from plist
  3. Get dictionary stored in array

    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"]);
    
    }
    
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • OK. how can i change the navigationbar color in storyboard?? actually i embedded on navigation controller and i wanna change its color to RGB(20,60,72) do you know??i tried that :self.navigationController.navigationBar.tintColor=[UIColor colorWithRed:26 green:62 blue:72 alpha:0]; its not working – Naveen Mar 23 '13 at 04:48
  • @Bhargavi I think the plist is in the bundle rather than the documents directory. You may consider editing the answer. – Anupdas Mar 23 '13 at 04:50
  • 1
    @Naveen Comments are for discussing doubts/clarification. If you have a new question ask as a seperate quesion. – Anupdas Mar 23 '13 at 04:52
  • @Anupdas have generated one plist file in that directory so I hace given that path. Anyways I mentioned to fetch it from bundle/ directory in either case – βhargavḯ Mar 23 '13 at 04:52
  • @Bhargavi When we make assumptions do provide the alternatives codes as well. – Anupdas Mar 23 '13 at 04:56
  • One comment, they won't be mutable dictionaries. – Carl Veazey Mar 23 '13 at 04:57
  • @Naveen I agree with Anupdas you should post question. Though for your answer try this self.navigationController.navigationBar.tintColor=[UIColor colorWithRed:26/255.0 green:62/255.0 blue:72/255.0 alpha:1]; – βhargavḯ Mar 23 '13 at 04:59
  • @thanks Bhargavi..I tried to post question ,but it shows"not meets our quality standards" i dnt like that – Naveen Mar 23 '13 at 05:03
  • @ITs WOrking...oh NOw i realise its a cgfloat so we have to use 255.0 ...thabks Bhargavi... – Naveen Mar 23 '13 at 05:14
  • @Naveen its also about alpha:0.. you need to set alpha:1. alpha is used for transparency. – βhargavḯ Mar 23 '13 at 05:20
  • @Bhargavi Please check this question http://stackoverflow.com/questions/15583805/usernotes-in-ios – Naveen Mar 23 '13 at 05:41
0

PropertyListDecoder can be used to decode plist file directly to object. For details see this answer https://stackoverflow.com/a/60389142/5662893

Imran
  • 3,045
  • 2
  • 22
  • 33
-1

do add .plist file in Targets => Copy bundle resource With the above in reply

tushar
  • 85
  • 1
  • 1
  • 5