How to read Array from plist iOS

I am trying to read plist which contains array

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

when i try to read valueForKey:@”keyarray1″, I get null value.
I tried to read as a string and array nut no use.

  • Custom UITableViewCell sometimes not responding
  • resize UISearchDisplayController dimmed black overlay
  • Obj-c generate a random float(or double) number between 0 and ∏ * 2
  • How to programmatically dismiss UIAlertController without any buttons?
  • AFNetworking problems with TLS Verification of a self signed server root CA
  • CoreData issue: -: unrecognized selector sent to instance
  • My Code

    NSDictionary * values=[[NSDictionary alloc] initWithContentsOfFile:@"values.plist"];
    NSArray *arrayValues=[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]];

    5 Solutions Collect From Internet About “How to read Array from plist iOS”

    First of all Check your plist looks like:

    enter image description here

    Now write following lines where you are accessing your plist

    Objectiv C:

    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Values" ofType:@"plist"]];
    NSLog(@"dictionary = %@", dictionary);
    NSArray *array = [dictionary objectForKey:@"keyarray1"];
    NSLog(@"array = %@", array);

    Here is the complete screen shot (with log output) of my work window:

    enter image description here


    let dictionary = NSDictionary(contentsOfFile: Bundle.main.pathForResource("Values", ofType: "plist")!);
    let array = dictionary?["arrayKey"] as! NSArray
    print("dictionary=",  dictionary, "\narray =",  array)

    enter image description here

    I know this question is too old and I’ll just like to add more information for those people encounter this recently.

    In my case I created a plist as Array(plist default is Dictionary with key “Root”).

    enter image description here

    then the xml looks like this:

    enter image description here

    On my view controller I initialize directly the Array instead of initializing the Dictionary then get object for key “Root”:

    enter image description here

    Note: I only wanted to add this info since I only see initializing the Dictionary then get object for keys. Hope it will help you guys.

    Where is the plist stored? Is it in the app bundle? If so, you probably want to use [[NSBundle mainBundle] pathForResource:@"values" ofType:@"plist"] to get the path, instead of hardcoding @"values.plist".

    After you have the path down correctly, you can then get the array from the dictionary without any problems, with something like [values objectForKey:@"keyarray"].

    Final note: there is a difference between objectForKey: and valueForKey:. You’re currently using valueForkey: which is part of NSKeyValueCoding, a protocol that NSDictionary happens to conform to. You should use objectForKey: (or syntactic sugar accessors, i.e. dictionary[@"key"]) instead, as they are the proper ways of accessing a value from a dictionary.

    Can you try following code?

    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"values" ofType:@"plist"];
    NSDictionary * values=[[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *arrayValues=[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]];
    NSLog(@"arrayValues = %@",arrayValues);

    I got following output in log:-

    arrayValues = (

    Try this

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"values.plist"];
    NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
    NSArray *arr =[dict valueForKey:@"keyarray1"];