NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath;
if (str_FolderName!=nil){
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]];
} else {
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;
if ([fileManager fileExistsAtPath: filePath])
{
//file is exist
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
} else {
BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
// sometime return YES, sometime return NO <<< this is my question
if(Success == YES){
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
data = [[NSMutableDictionary alloc] init];
}
}
for (uint32_t u32_Cnt=0;u32_Cnt<arr_Keys.count;u32_Cnt++) {
[data setValue:[NSString stringWithFormat:@"%@",[marr_Data objectAtIndex:u32_Cnt]] forKey:[arr_Keys objectAtIndex:u32_Cnt]];
}
if ([data writeToFile:filePath atomically: YES])
{
NSLog(@"[Save OK]");
return DEF_PASS;
} else {
NSLog(@"[Save Fail]"); <<<< now happened
}如果我检查过的文件不存在,那么我想要创建文件为filepath。
我的设备有时成功,有时失败
Filepath如下:
@"/var/mobile/Containers/Data/Application/xxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/Documents/mfolder/myfile“
有人能帮我吗?
发布于 2017-07-06 08:33:02
我更新了你的一些代码。我为我妥善分配了NSFilemanager及其工作。您可以在文件路径或filemanager对象中出现问题。
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath;
if (str_FolderName!=nil){
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]];
}else{
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;
if ([fileManager fileExistsAtPath: filePath])
{
//file is exist
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}
else
{
if (str_FolderName!=nil){
NSString *strDicPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FolderName]];
// You need to create director before creating file.
if (![[NSFileManager defaultManager] fileExistsAtPath:strDicPath])
{
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:strDicPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
}
}
BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
// sometime return YES, sometime return NO <<< this is my question
if(Success == YES){
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
data = [[NSMutableDictionary alloc] init];
}
}我还在模拟器和设备中检查了这段代码。
用于将字符串写入文件:
NSError *error;
NSString *stringToWrite = @"1\n2\n3\n4";
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];https://stackoverflow.com/questions/44942871
复制相似问题