How do I format a text file to be read in using arrayWithContentsOfFile
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.
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]
Just for completeness on bShirley's answer, if you wanted to do this using the pList editor:
- Create a new plist file in your project.
- Make sure it is added to your bundle in the target build phase settings.
- Add one element to the plist, right click it and select type = array.
- Add array elements as needed.
- save and then edit the plist file in text editor
- remove the
<dict>
,</dict>
, and<key> </key>
lines, for the data you should end with<array>...your elements...</array>
- load in code via:
[[NSMutableArray alloc] initWithContentsOfFile:path]
or[NSMutableArray arrayWithContentsofFile:path]
. Remember to retain or autorelease the second one if you use it.
Example working plist:
<?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>Every 10 Minutes</string>
<string>Every 20 Minutes</string>
</array>
</plist>
this worked for me in Xcode 4.2 on iPad2 with iOS 5.01
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.
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;
}
If you create a plist in Xcode (definitely in 4.1) it will ALWAYS be a Dictionary. If you want to load it as an Array, you can manually edit the file (open it in TextEdit) and remove the <dict> </dict>
and the key to your single entry that is an Array. You can now open it back up in Xcode and edit it like normal.
Apple fail on this one!
精彩评论