Search code examples
objective-cnsarray

Objective-C data management


I am kind of new at Objective-C, but managed to get my project up and running. The only question I have is regarding data management. I have two view controller each with an array containing the titles for the table cells within them. At the moment these titles are defined in the corresponding .m file under "viewDidLoad". I would really like to pre-define these arrays in a separate file, if possible.

Could one of you let me know if it is possible, and if so, how I would go about implementing this?


Solution

  • Yes, it is possible, usually we use .plist files.

    For example, if you have a file items.plist with the contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <array>
        <string>Item1</string>
        <string>Item2</string>
        <string>Item3</string>
    </array>
    </plist>
    

    You can read it like this:

    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"items" ofType:@"plist"]];
    
    NSLog(@"array = %@", array);