NSMutableDictionary 不保存到本地目录

NSMutableDictionary not saving to local directory

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

这是我所拥有的,它实质上是创建一个列表,列出哪些文件已上传到 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 分配初始化,我得到了相同的结果。有什么想法吗?

下一行有问题

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

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

解决方案

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

另外,利用writeToFile return参数来验证。

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

好的,所以我的问题是我将 NSNumbers 作为键,但它不起作用。我制作了钥匙串,我们是金色的。