iOS:NSUserDefault 用于 UIImage 数组

发布于 2024-12-29 14:49:48 字数 802 浏览 0 评论 0原文

我想存储 UIImage 数组,我这样做:

//in didFinishLaunchingWithOption

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"theKey"];
if (data == NULL)  arrayImage = [[NSMutableArray alloc] init];
else {arrayImage = [[NSMutableArray alloc] init]; arrayImage = [NSKeyedUnarchiver unarchiveObjectWithData:data];}
NSLog(@"arrayImage:%@", arrayImage);

//and in applicationDidEnterBackground

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arrayImage];
[defaults setObject:data forKey:@"theKey"];
NSLog(@"arrayImage:%@", arrayImage);

当应用程序在 nslog 中的 didFinishLaunchingWithOption 中运行时,我看到数组中的所有对象,但是当我使用它时,我发生崩溃,显示“[__NSArrayM count]:消息发送到已释放实例”为什么?

I want to store an array of UIImage and I do this:

//in didFinishLaunchingWithOption

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"theKey"];
if (data == NULL)  arrayImage = [[NSMutableArray alloc] init];
else {arrayImage = [[NSMutableArray alloc] init]; arrayImage = [NSKeyedUnarchiver unarchiveObjectWithData:data];}
NSLog(@"arrayImage:%@", arrayImage);

//and in applicationDidEnterBackground

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arrayImage];
[defaults setObject:data forKey:@"theKey"];
NSLog(@"arrayImage:%@", arrayImage);

when app run in didFinishLaunchingWithOption in nslog I see all object in my array, but when I use it, I have a crash that say "[__NSArrayM count]: message sent to deallocated instance" why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2025-01-05 14:49:48

我不完全确定,但我认为

+(id)unarchiveObjectWithData:(NSData *)数据

为您提供一个自动释放的对象,因此您可以保留它。我认为它会给你一个不可变的对象,所以当你尝试从中添加或删除对象时,你会得到一个错误(我对此不确定,但我想我曾经遇到过这种情况.. .)

我会重写你的代码的某些部分:

...
if (data == nil)
{
    arrayImage = [[NSMutableArray alloc] init];
} else
{
    //arrayImage = [[NSMutableArray alloc] init]; //why to allocate and initialize if you are goind to unarchive it?
    //arrayImage = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain]; //note the retain here
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data]; //the unarchive won't mantain mutability (I guess).
    arrayImage = [NSMutableArray arrayWithArray:arr]; //create a mutable copy
}
...

I'm not completely sure, but I think that

+(id)unarchiveObjectWithData:(NSData *)data

gives you an autoreleased object, so you may retain it. And I think it will give you a non-mutable object, so when you will try to add or remove objects from it you will get an error (I'm no sure about this, but I think I once was in this situation...)

I would rewrite some part of your code:

...
if (data == nil)
{
    arrayImage = [[NSMutableArray alloc] init];
} else
{
    //arrayImage = [[NSMutableArray alloc] init]; //why to allocate and initialize if you are goind to unarchive it?
    //arrayImage = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain]; //note the retain here
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data]; //the unarchive won't mantain mutability (I guess).
    arrayImage = [NSMutableArray arrayWithArray:arr]; //create a mutable copy
}
...
离线来电— 2025-01-05 14:49:48

我假设你没有使用 ARC。问题在于:

if (data == NULL)
  arrayImage = [[NSMutableArray alloc] init];
else {
  arrayImage = [[NSMutableArray alloc] init];
  // HERE
  arrayImage = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

HERE 处,您将 arrayImage 中的值替换为键控解档器的新实例。您之前init的值丢失了(事实上,泄漏了)。来自解档器的值是一个自动释放的对象,因此当池耗尽时将被释放。这是在进行 applicationDidEnterBackground 调用之前。

正确的解决方案是保留解档器中的值。即:

if (data == nil)
  arrayImage = [[NSMutableArray alloc] init];
else
  arrayImage = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain];

I assume you're not using ARC. The problem is in:

if (data == NULL)
  arrayImage = [[NSMutableArray alloc] init];
else {
  arrayImage = [[NSMutableArray alloc] init];
  // HERE
  arrayImage = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

At HERE, you replace the value in arrayImage with a new instance for the keyed unarchiver. The value you init'd just previously is lost (in fact, leaked). The value from the unarchiver is an autoreleased object, so will be released when the pool is drained. This is before the applicationDidEnterBackground call is made.

The correct solution is to retain the value from the unarchiver. Viz:

if (data == nil)
  arrayImage = [[NSMutableArray alloc] init];
else
  arrayImage = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文