Question

for(int x = 0; x < [tags count]; x++){
    NSString* tagsValue = [[NSString alloc] initWithFormat:@"%d: %f", 1, 
    [[tags objectAtIndex:x]doubleValue]];

    [[tagsValue dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath 
    atomically:NO];
}

i understand that writeToFile will replace the file itself so it would of give the final value of the array value. How could i approach this i've been trying to get this around my head for a few hours but i've had no luck thanks! :)

Was it helpful?

Solution

I think you are trying to write directly array to file.

  1. convert your array in string format.

    NSString *tagsCompleteStr = [tags componentJoinedByString:@" "];
    [tagsCompleteStr  writeToFile:fileAtPath 
                                           atomically:NO];
    

Hope this will help.

OTHER TIPS

The following code will add the tagsValue string at the end of the file,
Make sure you have already created the file.

   for(int x = 0; x < [tags count]; x++){
       NSString* tagsValue = [[NSString alloc] initWithFormat:@"%d: %f", 1, [[tags objectAtIndex:x]doubleValue]];
       NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:fileAtPath];
       [myHandle seekToEndOfFile];
       [myHandle writeData:[tagsValue dataUsingEncoding:NSUTF8StringEncoding]];
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top