我有一个NSDictionary,它包含一堆具有NSDates的数组,每个数组包含可变数量的NSStrings
像这样的东西:
NSDictionary
Key: 1999-whatever-whatever
Object: (Array)
"Some string"
"Some other string"
Key: 2000-whatever-whatever
Object: (Array)
"Some third string"
Run Code Online (Sandbox Code Playgroud)
我一直在寻找toToFile,但它似乎并不支持NSDictionarys有密钥的NSDates,所以我查看了KeydArchiver,这是最好的方法吗?
如果是,那么关于该主题的任何教程都会有所帮助.
谢谢你的时间.
PS:我假设我应该在ApplicationWillTerminate上保存?
编辑 - 我没有意识到你的问题说iOS.因此,忽略我将字典保存到桌面的方式,但将NSDate保存为字符串的方法是相同的.
如果更改保存NSDictionary的方式,则无需进入KeydArchiver.
NSDictionary
Key: 1999-08-13 //<----- Have this as a string in your dictionary. Not NSDate
Object: (Array)
"Some string"
"Some other string"
Key: 2000-08-13 //<------Have this as a string in your dictionary. Not NSDate
Object: (Array)
"Some third string"
Run Code Online (Sandbox Code Playgroud)
如果要将NSDictionary保存到文件,可以这样做:
[MyDictionary writeToFile:@"/Users/yourUsername/Desktop/mySavedDictionary.plist" atomically:YES];
Run Code Online (Sandbox Code Playgroud)
如果要再次加载字典:
NSDictionary *myDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/Users/yourUsername/Desktop/mySavedDictionary.plist"];
Run Code Online (Sandbox Code Playgroud)
加载字典后,假设您想要在2000-08-13日期存储数据,那么您可以这样做:
首先我假设NSDate myPreviouslyDeclaredDate是你想要在字典中查找的日期,并且它的格式为yyyy-MM-dd.您可以选择您想要的格式.使用AppleDocs作为指南.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *previouslyDeclaredDateInStringForm = [dateFormatter dateFromString:myPreviouslyDeclaredDate];
id objectNeededFromDictionary = [myDictionary objectForKey:previouslyDeclaredDateInStringForm];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1217 次 |
| 最近记录: |