我一直在尝试将字符串写入iOS5.1 中的文件,但文件内容始终为(null)。你能帮我找出原因吗?这是我的代码:
NSString *dir = [NSHomeDirectory() stringByAppendingPathComponent:@"mydirectory"];
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: 64];
for (int i=0; i<DEFAULT_LENGHT_OF_NOTE_FILE_ID; i++)
{
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
}
NSString* randomFileName = [NSString stringWithFormat:@"%@.html",randomString];
NSString *filePath = [dir stringByAppendingPathComponent:randomFileName];
NSString *str = @"Mystr";
//save content to the documents directory
[str writeToFile:filePath atomically:NO encoding:NSStringEncodingConversionAllowLossy
error:nil];
NSString *content = [[NSString alloc] initWithContentsOfFile:filePath
usedEncoding:nil
error:nil];
NSLog(@"%@", content);
NSLog 总是返回 (null)。我究竟做错了什么?
真挚地,
佐利