views:

245

answers:

1

im getting a "CFDictionaryAddValue(): immutable collection 0xd5aea0 given to mutating function"

error when i try to write a string to a file using the follwowing code

NSString *xmlString = [NSString stringWithString:xmlData];
NSError *error = nil;
if ([xmlString writeToFile:filePath atomically:NO encoding:NSASCIIStringEncoding error:&error])

xmlData was a mutable string but xmlString is not.

any ideas?

A: 

It works for me in cocoa.

NSString * xmlData = @"This is some random string";
NSString * xmlString = [NSString stringWithString:xmlData];
NSError * error = nil;
if (![xmlString writeToFile:@"data.txt"
                 atomically:NO
                   encoding:NSASCIIStringEncoding
                      error:&error])
{
    NSLog(@"writeToFile failed: %@", error);
}

I would check:

  • How do you get xmlData? Is it a NSString?
  • Do you specify file path within your app bundle? You will not be able to write outside your application directory apart from Documents (?) I think.

This is how you would specify file in Documents directory:

// Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

// <Application Home>/Documents/foo.plist
NSString *fooPath = [documentsPath stringByAppendingPathComponent:@“foo.plist”];
stefanB