views:

334

answers:

3

I have several large word lists and I had been loading them in place in the code as follows:

NSArray *dict3 = [[NSArray alloc] initWithObjects:@"abled",@"about",@"above",@"absurd",@"absurdity", ...

but now it is actually causing an exc_bad_access error weirdly, i know it seems implausible but for some reason IT IS causing it. (trust me on this I just spend the better part of a day debugging this crap)

Anyway, I'm looking to get these humongous lines of code out of my files but I'm not sure what the best approach is. I guess I could do a plist but I need to figure out how to automate the process.

it be easiest if I could just use the text files I've been compiling so if anyone knows how I can format the text file so that the

myArray = [NSArray arrayWithContentsOfFile: @"myTextFile.txt"];

will be read in correctly as one word per element in the array it would really be appreciated.

Thanks,

Nick

+2  A: 

myTextFile.txt should be in .plist format, so use the XCode Plist editor to create it and fill it up with data.

From the docs:

arrayWithContentsOfFile:

  • (id)arrayWithContentsOfFile:(NSString *)aPath

Creates and returns an array containing the contents of the file specified by aPath. The file identified by aPath must contain a string representation produced by the writeToFile:atomically: method. In addition, the array representation must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects).

Returns nil if the file can't be opened or if the contents of the file can't be parsed into an array.

//Use

[array writeToFile:path atomically:YES];

//to write your array directly, or

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];

//or

NSString *error;
NSData *data = [NSPropertyListSerialization dataFromPropertyList:array format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];

//for simpler data (arrays of strings)

//to write from NSData instead

[data writeToFile:path atomically:YES]
luvieere
i have like thousands of entries Is there any way to do this programatically from nsarrays?
nickthedude
@nickthedude Yes, it says so in the docs, as I quoted: use NSData's `writeToFile:atomically:`. Edited to add some code.
luvieere
+1  A: 

Check the documentation for NSArray:

+ (id)arrayWithContentsOfFile:(NSString *)aPath

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

The file must be written by writeToFile:atomically: in order to load from it.

Raj
A: 

I ended up scavenging through a friends code and found my answer, nice little piece of code actually:

- (NSArray *) readFile:(NSString *) path {


NSString *file = [NSString stringWithContentsOfFile:path];

NSMutableArray *dict = [[NSMutableArray alloc] init];
NSCharacterSet *cs = [NSCharacterSet newlineCharacterSet];
NSScanner *scanner = [NSScanner scannerWithString:file];

NSString *line;
while(![scanner isAtEnd]) {
    if([scanner scanUpToCharactersFromSet:cs intoString:&line]) {
        NSString *copy = [NSString stringWithString:line];
        [dict addObject:copy];

    }
}
NSArray *newArray = [[NSArray alloc] initWithArray:dict];

return newArray;
}
nickthedude