Friday, September 6, 2013

Reading file from resource in iOS

I was working on creating some utility function for iOS App. I required to create a File in resource and read it. File contains some words separated by new lines. I wanted to read those words and create array of those.

Its quite simple but though to share it.

Following how its done.

        
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"keywords_en" ofType:@"txt"];
NSError *error = nil;
NSString *keywordsStr = [NSString stringWithContentsOfFile: filePath encoding:NSUTF8StringEncoding error:&error];

NSArray* keywordArray = [[keywordsStr componentsSeparatedByString:@"\n"]retain];

No comments:

Post a Comment