Saving an Mutable Array into file via writeToFile

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! :)

Answers


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.


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]];
   }

Need Your Help

Max Image Width and Height in Xcode?

iphone xcode image height width

I could have sworn that I seen a while back that the max image size you can load into Xcode was 1024 x 1024, meaning you can't use images that are larger than this, but I can't remember where I see...

Rspec-view-file doesn't know about my routes

ruby-on-rails ruby ruby-on-rails-3 rspec rspec-rails

I'm new in rspec, write rspec-test for View and there's a problem:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.