nsarray to nsdata

Discussion in 'Mac Programming' started by BollywooD, Oct 25, 2009.

  1. macrumors 6502

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #1
    I have an nsarray of strings i would like to save as a text file of strings on separate lines.

    I am using NSFileManager createFileAtPath, and I need to convert the NSArray into NSData first. I have tried this:

    Code:
    NSData *data = [NSArchiver archivedDataWithRootObject:filters];
    but this doesnt give the desired results.....

    here is the code i am using:

    Code:
    NSData *data = [NSArchiver archivedDataWithRootObject:myArray];
    NSFileManager *fm = [NSFileManager defaultManager];
    			[fm createFileAtPath:[savePanel filename] contents:data attributes:nil];
    where myArray, is simply an array of strings.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    If you want just plain text, use componentsJoinedByString: to create an NSString from the array with each item separated by a new line. Then use writeToFile:atomically:encoding:error: to write it to file.
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    See the NSArray method -componentsJoinedByString:.

    Edit: I see kainjow a few minutes earlier.
     
  4. thread starter macrumors 6502

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #4
    thanks kainjow, that worked perfectly!
    :D
     

Share This Page