开发者

Reading the contents of the file to an array

How to read the Contents of the CSV file to an array in the objective c.. I Have a C开发者_JAVA技巧SV file where i need to read the contents of it in a array.


Try this - Here, I expect a file array.out in documents directory. This file I read it into an NSArray and iterate through it...

// Get path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
  NSUserDomainMask, YES);
if([paths count] > 0)
{
  // Path to save array data
  NSString  *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"];

  // Read both back in new collections
  NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];

  for (NSString *element in arrayFromFile) 
    NSLog(@"Beer: %@", element);
}


NSString provides some high level support for this.

to read the file, use something like this:

NSString * file = [[NSString alloc] initWithContentsOfFile:path
    encoding:encoding error:&error];

then to divide the string, use something like:

NSArray * values = [file componentsSeparatedByString:separator];

or

NSArray * values = [file componentsSeparatedByCharactersInSet:separatorSet];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜