I’ve been trying to write a string into a file in iOS5.1, but the file content is always (null). Can you help me figure out the reason?
Here’s my code:
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);
The NSLog always returns (null). What am i doing wrong?
Sincerely,
Zoli
Change the following
to