Very simple lookup of a Plist in Xcode 5

I want to do a very simple task with a Plist, but I'm very new to all this. I have a variable integer, x , in the app and want to look up the integer in the first array of the PList and return a corresponding string from the second array. The filename of the plist is AApropertylist.plist and its XML version is thus:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
    <string>Text of 1</string>
    <string>Text of 2</string>
    <string>Text of 3</string>

(this is a very truncated version, both arrays go up to 45000)

The "Text of 1" "Text of 2" is just placeholder text, the real data is still being compiled. The goal is the app will display the text result on a label

I would really appreciate some help, I've looked everywhere but I suspect the solution is so simple noone has bothered to write a tutorial, I can only find stuff pertaining to more complex Plist structures and table views etc.

thanks in anticipation!



You could just use a dictionary with the numbers as the key, it would save you having to load two relatively large arrays. You could do it something like this:

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

Then to load it nice and simply:

NSString *path = [[NSBundle mainBundle] pathForResource:@"AApropertylist" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSString *returnedString = [dict objectForKey:@"ANY_NUMBER"];

NSObject myPlistContent = [NSDictionary dictionaryWithContentsOfFile:filename];

But double check whether it is really an NSDictionary that your receive. I think the root object would always be a dict but I do not sign that in blood. The Dictionary may contain further Dictionaries, Arrays, Strings, Dates, Data and Numbers which depend on the contents and structure of the plist.

You should load the plist and have it as an NSDictionary (or array) here is a quick code to get you in the right direction

// path = the path which your plist file is located
NSString* path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];

// Break the plist file into an array so we can play with the data
NSArray* a = [NSArray arrayWithContentsOfFile:path]; 
// now you have the plist as an array (it is called a)

// next line is a loop which runs on our array (a) and each element is becoming an NSDictionary called d

for (NSDictionary *d in a)

    // just take the value for a single element (d) with the value "number"
    // store it in a string called cdata
    NSString *cdate = [d objectForKey:@"number"];
   // if this value is equal to "something" do your thing
    if ([cdate isEqualToString:@"SOMTHING"])

        // do something with text here

Need Your Help

Castle.Windsor: ResolveAll with name

c# inversion-of-control castle-windsor

I am new to using Castle.Windsor for IoC. Right now, I want to register UI elements to plug into certain points within the main form (SWF). Thus I have tree nodes to inject those elements into, suc...

How to get row index of a gridview row clicked by a user on it by javascript? javascript gridview click

How to get row index of a gridview row clicked by a user on it by javascript ?