NSMutableDictionary不保存到本地目录

时间:2015-02-19 04:19:37

标签: objective-c nsmutabledictionary writetofile

我测试一个目录是否正在保存,但它似乎不是。

以下是我所拥有的内容,它实质上是创建了一个已上传到Dropbox的文件列表。 DropboxList在头文件中声明,然后同步。

int i = 0;

DropboxList = [[NSMutableDictionary alloc]init];

do {

[DropboxList setObject:@"NO" forKey:[NSNumber numberWithInt:i]];

i++;

} while (i < sortedFiles.count);

NSLog(@"dropbox list is %@", DropboxList);


NSArray *dropBoxPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dropBoxDocumentsDirectory = [dropBoxPaths objectAtIndex:0];
NSString *dropBoxDataPath = [dropBoxDocumentsDirectory stringByAppendingPathComponent:@"/DropboxUploads"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dropBoxDataPath]) {
    [[NSFileManager defaultManager] createDirectoryAtPath:dropBoxDataPath withIntermediateDirectories:NO attributes:nil error:nil];
}

NSString *filePath = [dropBoxDataPath stringByAppendingPathComponent:@"dropboxList.out"];

[DropboxList writeToFile:filePath atomically:YES];

NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

NSLog(@"newDictionary list is %@", newDictionary);

这是我的控制台:

dropbox list is {
    0 = NO;
    3 = NO;
    2 = NO;
    1 = NO;
    4 = NO;
}
newDictionary list is (null)

我在写之前尝试过为newDictionary分配init,我得到了相同的结果。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

问题在于以下一行

[DropboxList setObject:@"NO" forKey:[NSNumber numberWithInt:i]];

DropboxList writeToFile仅适用于有效的属性列表。要使NSMutableDictionary成为有效的属性列表对象,其键应为字符串类型而不是NSNumber

<强>解决方案

[DropboxList setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",i]]];

另外,请使用writeToFile返回参数进行验证。

BOOL isWritten = [DropboxList writeToFile:filePath atomically:YES];
if (isWritten)
{
    NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    NSLog(@"newDictionary list is %@", newDictionary); 
{
else
{
    NSLog(@"Something went wrong");
}

答案 1 :(得分:0)

好的,我的问题是我将NSNumbers作为键,这不起作用。我做了钥匙串,我们是金色的。