IOS/objective-C : Save Custom Array as NSUserDefault

标签 ios nsarray nsuserdefaults

我试图将对象数组保存到 NSUserDefault 中,但没有成功。当我在尝试之前注销数组时,它充满了对象。但是,当我尝试注销 NSUserDefault 时,它是 NULL。谁能看到我可能做错了什么?感谢您的任何建议:

 Items *myItems = [mutableFetchedObjects mutableCopy];
    NSLog(@"my Items%@",myItems);//LOGS OUT LONG LIST OF ITEMS
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myItems];
    [currentDefaults setObject:data forKey:@"myItems"];
[currentDefaults synchronize];
    Items *myRetrievedItems = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"myItems"] mutableCopy];
    NSLog(@"my Retrieved Items%@",myRetrievedItems); //LOGS OUT AS NULL

最佳答案

正如提到的其他答案,这是因为您的数组不符合 NSDictionary 类型(字符串、二进制、 bool 等)。您的数组成员属于自定义类型,因此无法保存。您需要做的是先将数组转换为二进制,然后保存。

关于IOS/objective-C : Save Custom Array as NSUserDefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46415490/

相关文章:

ios - NSUserDefaults boolForKey

objective-c - cocoa : Resetting NSUserDefaults

ios - 有没有办法清除安装应用程序时存储的所有数据?

ios - AVAudioSession 未知

ios - 通过 Cocoapods 设置 CVCalendar : Views Aren't Showing

iphone - NSArray 排序和隔离

ios - Plist 值被替换而不是添加

iphone - 将 UITabBarItem 添加到 UIViewController 中的 UITabBar

ios - numberOfRowsInSection/3 - 只返回 15 张图像而不是 16 或 17,因为它们不能被 3 整除

iphone - 计算 NSArray 中 NSDictionary 中的值