我更新Xcode开发,并试图保存我的应用程序的状态跟踪多个索引集,整数和字符串。我已经尝试了很多不同的代码,并且无法将它保存到.plist中。保存以下数据类型的最佳方法是什么?NSMutableIndexSets
和NSUIntegers
?任何方向都很好,谢谢。iPhone应用程序保存状态 - nsmutableindexset和nsuintegers?
0
A
回答
0
您的问题的简短答案是,您不能将索引集保存到plist或用户默认值。有一个非常短的对象类型列表,您可以写入plist。在Xcode中查找NSDictionary类的文档,并搜索字符串“属性列表对象”这就是他们告诉你哪些对象可以写入正确列表的地方。对象类型是NSString,NSData,NSDate,NSNumber,NSArray或NSDictionary对象。
Omar Abdelhafith发布了一个非常冗长和复杂的代码块来将索引集转换为数组,这应该可行。
然而,有一种更简单的方法。的NSIndexSet符合NSCoding协议,这意味着你可以将其转换为NSData的单次调用:
NSData *setData = [NSKeyedArchiver archivedDataWithRootObject: mySet];
并把它放回索引集:
NSIndexSet *setFromData= [NSKeyedUnarchiver unarchiveObjectWithData: setData];
NSMutableIndexSet *mutableSet = [setFromData mutableCopy];
注意,所有的这些方法,如果你从一个可变对象(set,array,dictionary等)开始,那么当你读回它时,你得到的对象将是一个不可变的版本。您必须手动将其转换为可变版本。大多数具有可变类型的对象都支持mutableCopy方法。
0
使用下面的代码
//Saving
NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
[set addIndex:1];
[set addIndex:2];
NSMutableArray *arrToSave = [[NSMutableArray alloc] init];
NSUInteger currentIndex = [set firstIndex];
while (currentIndex != NSNotFound)
{
[arrToSave addObject:[NSNumber numberWithInt:currentIndex]];
currentIndex = [set indexGreaterThanIndex:currentIndex];
}
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
NSUInteger integer = 100;
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/savePath.txt"];
[dic setValue:arrToSave forKey:@"set"];
[dic setValue:[NSNumber numberWithUnsignedInt:integer] forKey:@"int"];
[dic writeToFile:savePath atomically:YES];
//Loading
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/savePath.txt"];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:savePath];
NSArray *arr = [dic valueForKey:@"set"];
NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
[set addIndex:[[arr objectAtIndex:0] unsignedIntValue]];
[set addIndex:[[arr objectAtIndex:1] unsignedIntValue]];
NSUInteger integer = [[dic valueForKey:@"int"] unsignedIntValue];
0
据我所知有可以归档到plist文件中项目的设置列表。 从内存(这意味着你应该去查看它的文档)它是NSString,NSArray,NSDictionary,NSData,NSNumber和...我记不起来的其他几个。这一点是你的索引集可能不是其中之一,所以你需要将其转换为其他东西,将其归档并唤醒后将其解压缩并重新恢复。
相关问题
- 1. 如何在iPhone中使用NSUserDefaults保存应用程序状态?
- 2. android应用程序保存状态
- 3. 保存AngularJS应用程序的状态
- 4. 保存应用程序的状态
- 5. 在iPhone应用程序中何处保存状态
- 6. 保存Android应用程序状态
- 7. 应用程序状态保存/恢复
- 8. 保存应用程序状态
- 9. Android应用程序 - 保存状态
- 10. iPhone应用程序保存视图状态
- 11. Web应用程序和内存状态
- 12. Jython保存程序状态
- 13. 如何在终止应用程序之前保留iPhone应用程序状态?
- 14. 应用程序状态保存和恢复中的MonoTouch
- 15. iOS6应用程序状态保存和恢复
- 16. iPhone - 保存UIImageView状态
- 17. iphone避免保存状态
- 18. iPhone应用程序 - 隐藏状态栏
- 19. 维持iPhone应用程序的状态
- 20. 使用深度UINavigationController保存iPhone程序状态
- 21. 如何动态保存网络应用程序状态?
- 22. 应用主题后Android保存应用程序状态
- 23. iPhone应用程序中的状态模式(内存使用)
- 24. 退出后保留iPhone应用程序的状态
- 25. 保存应用状态
- 26. 我可以在iPhone OS中保存我的应用程序的状态吗?
- 27. 应用程序状态,数据在内存中保存多久?
- 28. 应用程序保持关闭状态
- 29. iPhone +在任何中断的情况下保存应用程序状态
- 30. 如何在应用程序终止时保存应用程序的状态?
这工作,并减少了所需的代码。谢谢,邓肯。 – rossi