How do I format a text file to be read in using arrayWithContentsOfFile

痞子三分冷 提交于 2019-12-01 00:19:45

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]
Edward

Just for completeness on bShirley's answer, if you wanted to do this using the pList editor:

  1. Create a new plist file in your project.
  2. Make sure it is added to your bundle in the target build phase settings.
  3. Add one element to the plist, right click it and select type = array.
  4. Add array elements as needed.
  5. save and then edit the plist file in text editor
  6. remove the <dict>, </dict>, and <key> </key> lines, for the data you should end with <array>...your elements...</array>
  7. 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;
}
bshirley

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!