views:

88

answers:

1

Hi, I do a iphone apps and I try to write in a file like this:

data = [NSMutableData dataWithBytes:LogString length:[LogString length]];
file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[file seekToEndOfFile];
[file writeData: data];
[file closeFile];

But the problem is that if I try to write the string "awf", the content of the file is "†∂…‡

+1  A: 

You're not using LogString correctly. I'm assuming it's a string, since that's the logical choice for an object with a -length method (and that's not already an NSData).

If that's the case, you should do:

data = [LogString dataUsingEncoding:NSUTF8StringEncoding];
file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[file seekToEndOfFile];
[file writeData: data];
[file closeFile];
Dave DeLong
Hey this is work good, but I have a warning, "Incompatile objective-c types assigning "struct nsdata", expected struct nsmutable data. Do you think this is a problem?
Alex
@Alex you only need to declare `data` as an `NSMutableData` if you're going to change the contents afterwards. In the code you posted, you don't need it to be mutable.
Dave DeLong