Objective c 从文件读取。Plist返回Null

Objective c 从文件读取。Plist返回Null,objective-c,plist,nsfilemanager,writetofile,Objective C,Plist,Nsfilemanager,Writetofile,为不同信息创建多个Plist路径的程序 但只有一条路行不通。 (我认为“writeToFile”是个问题) 代码: -(NSString *) createPath:(NSString *)withFileName { NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths

为不同信息创建多个Plist路径的程序

但只有一条路行不通。 (我认为“writeToFile”是个问题)

代码:

-(NSString *) createPath:(NSString *)withFileName
{
   NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *path = [documentsDirectory stringByAppendingPathComponent:withFileName];
   return path;
}
路径

NSLog = /var/mobile/Applications/02CABC0A-6B5B-4097-A9D1-4336BE8230B7/Documents/MessagesDB.plist
&

“ReturnsInfo”数组为空:/


有人需要帮忙吗?

我已经存储了以下数组,其中包含5个对象,我这边的效果很好,试试吧

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];
for(int i=0;i<5;i++)
    [array addObject:@"This is Demo String, You can write your own String here"];

NSString *_fileMessagesDB = @"MessagesDB.plist";
// save it to the file for persistency
NSString *messagesDB_Path = [self createPath:_fileMessagesDB];
[array writeToFile:messagesDB_Path atomically:YES];
NSMutableArray *ReturnsInfo = [[NSMutableArray alloc ]initWithContentsOfFile:messagesDB_Path];
NSLog(@"ReturnsInfo is : %@", ReturnsInfo);
NSMutableArray*array=[[NSMutableArray alloc]initWithCapacity:5];

对于(int i=0;i我曾经有过相同的错误。
1) 检查目录列表中plist的名称以匹配编码的名称
2) 检查项目设置,从“生成设置”>“复制捆绑资源”中手动删除预先存在的plist,然后从列表中拖放。
3) 选择目录列表中的plist,检查实用工具侧栏,检查Identity&Type>Location是否有效
4) 如果删除了应用程序的“默认”plist aka bundle标识符,请添加复制构建阶段,选择目标,选择pref文件夹作为绝对路径检查“仅在安装时复制”

这解决了我返回空值的问题

如果捆绑标识上的所有操作都失败,则始终可以通过代码将plist复制到pref文件夹:

NSString *path = [@"~/Library/Preferences/com.MyCompany.MyApp.plist" stringByExpandingTildeInPath];

BOOL PrefsExist=[[NSFileManager defaultManager] fileExistsAtPath:path];
NSString *copyPrefsPath = [@"~/Library/Preferences/com.MyCompany.MyApp.plist" stringByExpandingTildeInPath];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (PrefsExist == 0)
    {
        // Copy the plist to prefs folder (one-time event)
        NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"com.MyCompany.MyApp" ofType:@"plist"];
        [fileManager copyItemAtPath:tessdataPath toPath:path error:&error];
    } else
    {
        // Read/Write the values from plist
    }

Ahmed&Mayur,感谢您编辑WriteFile:Atomicaly:返回布尔值,是什么_messagesDB是一个数组?检查plist是否确实是一个数组。这可能是一个字典。嗨,迪本,你的代码运行得很好,我在程序中检查了它,但我在我的项目中实现了它,我的数组数据仍然为空:/Array是否“强”有什么关系?或者只在实现文件中定义?可以是,使其非原子并保留。
NSString *path = [@"~/Library/Preferences/com.MyCompany.MyApp.plist" stringByExpandingTildeInPath];

BOOL PrefsExist=[[NSFileManager defaultManager] fileExistsAtPath:path];
NSString *copyPrefsPath = [@"~/Library/Preferences/com.MyCompany.MyApp.plist" stringByExpandingTildeInPath];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (PrefsExist == 0)
    {
        // Copy the plist to prefs folder (one-time event)
        NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"com.MyCompany.MyApp" ofType:@"plist"];
        [fileManager copyItemAtPath:tessdataPath toPath:path error:&error];
    } else
    {
        // Read/Write the values from plist
    }