【问题标题】:Read from file.Plist Returns Null从文件中读取。Plist 返回 Null
【发布时间】:2013-05-07 05:31:38
【问题描述】:

为不同信息创建多个 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”数组为空:/

有人帮忙吗?

【问题讨论】:

  • 艾哈迈德和马尤尔,感谢您的编辑
  • writeToFile:atomically: 返回一个 BOOL,它是什么? _messagesDB 是一个数组吗?
  • 检查您的 plist 实际上是一个数组。它可能是一本字典。

标签: objective-c plist nsfilemanager writetofile


【解决方案1】:

我已经存储了包含 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);

【讨论】:

  • 嗨 Dipen,你的代码工作正常,我在程序中检查了它,但我在我的项目中实现它,我的数组数据仍然为 NULL:/
  • Array 是否“强”有关系吗?还是只在实现文件中定义?
  • 可能是,使其成为非原子并保留。
【解决方案2】:

我曾经遇到过同样的错误。
1) 检查目录列表中 plist 的名称是否与您的编码匹配
2)检查项目设置,从“构建设置”>“复制捆绑资源”手动删除预先存在的plist,然后从列表中拖放。
3) 选择目录列表中的 plist,检查 Utilities 侧边栏,检查 Identity & Type > Location as valid
4) 如果您删除了应用程序的“默认”plist aka 包标识符,请添加复制构建阶段,选择目标,选择 pref 文件夹作为绝对路径检查“仅在安装时复制”

这解决了我返回 null 的问题。

如果捆绑标识符都失败了,您可以随时通过代码将 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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    相关资源
    最近更新 更多