开发者

Text in file should not be overwritten

开发者 https://www.devze.com 2023-01-13 04:19 出处:网络
I am using the following code to write in a file. fileName = [[NSBundle mainBundle] pathForResource: @\"user\" ofType: @\"txt\"];

I am using the following code to write in a file.

fileName = [[NSBundle mainBundle] pathForResource: @"user" ofType: @"txt"];
NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:fileNa开发者_如何学Gome];
[myHandle seekToEndOfFile];

NSData *dataName = [uname dataUsingEncoding: NSUnicodeStringEncoding];

if ([dataName writeToFile:fileName atomically:YES])
    NSLog(@"writeok");

[myHandle seekToEndOfFile];

It overwrites the file. Suppose the file already contains the string "box". Now when I write a new word, "Hello", then the file contains only "Hello", not "box".

What is the problem with the code?


You cannot write to files in the Bundle. As the Bundle is code signed, any attempt to change the files in the bundle would break the signing. You need to move the file to the Documents directory and work on it there.

0

精彩评论

暂无评论...
验证码 换一张
取 消