0

为不同信息创建多个 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

&

-(void) messagesDbFlush
{
    // save it to the file for persistency
    NSString *messagesDB_Path = [self createPath:_fileMessagesDB];
    [_messagesDB writeToFile:messagesDB_Path atomically:YES];
    NSMutableArray *ReturnsInfo = [[NSMutableArray alloc ]initWithContentsOfFile:messagesDB_Path];
    NSLog(@"ReturnsInfo is : %@", ReturnsInfo);
}

“ReturnsInfo”数组为空:/

有人请帮忙吗?

4

2 回答 2

1

我曾经有同样的错误。
1)检查目录列表中的 plist 名称是否与您的编码匹配
2)检查项目设置,从“构建设置”>“复制捆绑资源”中手动删除预先存在的 plist,然后从列表中拖放。
3) 选择目录列表中的 plist,检查 Utilities 侧边栏,检查 Identity & Type > Location as valid
4) 如果您删除了应用程序的“默认”plist aka bundle identifier,添加复制构建阶段,选择目标,选择 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
    }
于 2013-05-19T20:30:26.190 回答
0

我已经存储了以下包含 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);
于 2013-05-13T09:01:27.537 回答