Friday, May 31, 2013

NSArray initWithContentsOfFile example in Objective C (iOS).



NSArray initWithContentsOfFile

Initializes a newly allocated array with the contents of the file specified by a given path.

- (id)initWithContentsOfFile:(NSString *)aPath

Parameters
aPath
The path to a file containing a string representation of an array produced by the writeToFile:atomically: method.

Return Value of [NSArray initWithContentsOfFile]
An array initialized to contain the contents of the file specified by aPath or nil if the file can’t be opened or the contents of the file can’t be parsed into an array. The returned object might be different than the original receiver.

Discussion of [NSArray initWithContentsOfFile]
The array representation in the file identified by aPath must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). The objects contained by this array are immutable, even if the array is mutable.

NSArray initWithContentsOfFile example.
//Creating a file path under iOS:
//1) Search for the app's documents directory (copy+paste from Documentation)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//2) Create the full file path by appending the desired file name
NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];

//Load the array
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithContentsOfFile: yourArrayFileName];
if(yourArray == nil)
{
    //Array file didn't exist... create a new one
    yourArray = [[NSMutableArray alloc] initWithCapacity:10];

    //Fill with default values
}
...
//Use the content
...
//Save the array
[yourArray writeToFile:yourArrayFileName atomically:YES];

Example of [NSArray initWithContentsOfFile].
- (id)init {
        if (self = [super init]) {
                self.title = @"Table";

                NSBundle* bundle = [NSBundle mainBundle];
                NSString* plistPath = [bundle pathForResource:@"myDataBase"
ofType:@"plist"];
                NSDictionary* tmpDict = [[NSDictionary alloc]
initWithContentsOfFile:plistPath];
                NSArray *table = (NSArray *)[tmpDict objectForKey:@"itemTable"];

                TTListDataSource* tableItems = [[TTListDataSource alloc] init];

                for(NSDictionary *tmpDict in table){

                        NSString *color = [NSString stringWithFormat:@"%@", [tmpDict
objectForKey:@"itemColor"]];
                        NSString *sub = [NSString stringWithFormat:@"%@", [tmpDict
objectForKey:@"itemSub"]];
                        //NSString *sub2 = [NSString stringWithFormat:@"%@", [tmpDict
objectForKey:@"itemSub2"]];
                        NSString *img = [NSString stringWithFormat:@"bundle://%@", [tmpDict
objectForKey:@"itemImg"]];
                        NSString *url = [NSString stringWithFormat:@"tt://%@", [tmpDict
objectForKey:@"itemURL"]];

                        TTTableSubtitleItem *item = [TTTableSubtitleItem itemWithText:color
subtitle:sub imageURL:img URL:url];

                        [tableItems.items addObject:item];
                                }
                self.dataSource = [TTListDataSource
dataSourceWithItems:tableItems.items];

..............

NSArray initWithContentsOfFile example.
usernames = [[NSArray alloc] initWithContentsOfFile:[NSBundle
pathForResource:@"TwitterUsers" ofType:@"plist" inDirectory:[NSString
stringWithFormat:@"/%@/Presence2.app", NSHomeDirectory()]]];

End of NSArray initWithContentsOfFile example article.