我更新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