ableArray的第一个对象存储在NSUserDefaults

ableArray的第一个对象存储在NSUserDefaults

本文介绍了只有NSMutableArray的第一个对象存储在NSUserDefaults中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试存储UILocalNotification的队列以解决,它会归档和取消归档我的对象,但只有第一个。



如何存档我的NSMutableArray中的所有对象?



代码



  // init / unarchive queue 
if(self.queue == nil)
{
//尝试加载存储数组
NSUserDefaults * currentDefaults = NSUserDefaults standardUserDefaults];
NSData * dataRepresentingSavedArray = [currentDefaults objectForKey:@LocalNotificationQueue];
if(dataRepresentingSavedArray!= nil){
NSArray * oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if(oldSavedArray!= nil){
self.queue = [[NSMutableArray alloc] initWithArray:oldSavedArray];
}
else
{
self.queue = [[NSMutableArray alloc] init];
}
}
else
{
self.queue = [[NSMutableArray alloc] init];
}
}
// add
[self.queue addObject:notif];

//存储队列
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.queue] forKey:@LocalNotificationQueue];

如果我添加项目1,2,3。重新启动并加载。我只有3个。



添加1,2,3。重新启动并加载。我有3,1,2。



如果重要的话。这是一个Phonegap / Cordova CDVPlugin。

解决方案

之后

  [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.queue] forKey:@LocalNotificationQueue]; 

您需要致电

  [[NSUserDefaults standardUserDefaults] synchronize] 

保存用户默认值。 / p>

I am trying to store a queue of UILocalNotification to solve the limit problem. I used this approach and it does archive and unarchive my object but only the first one.

How do I archive all objects from my NSMutableArray?

Code

// init/unarchive queue
if (self.queue == nil)
{
    // try loading stored array
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"LocalNotificationQueue"];
    if (dataRepresentingSavedArray != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
        if (oldSavedArray != nil) {
            self.queue = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        }
        else
        {
            self.queue = [[NSMutableArray alloc] init];
        }
    }
    else
    {
        self.queue = [[NSMutableArray alloc] init];
    }
}
// add
[self.queue addObject:notif];

// store queue
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.queue] forKey:@"LocalNotificationQueue"];

If I add items 1,2,3. Restart and load. I have only 3.

Add 1,2,3. Restart and load. I have 3, 1, 2.

If it matters. This is a Phonegap/Cordova CDVPlugin.

解决方案

After

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.queue] forKey:@"LocalNotificationQueue"];

You need to call

[[NSUserDefaults standardUserDefaults] synchronize]

To save the user defaults.

这篇关于只有NSMutableArray的第一个对象存储在NSUserDefaults中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:02