初始化块内的对象

发布于 2024-12-21 22:43:01 字数 874 浏览 1 评论 0原文

我正在尝试初始化一个 dict 变量,但我不明白为什么一种方法有效,而另一种方法无效。

在情况 1 中,一切都很好,我稍后可以使用 dict。
在情况 2 中,它很快就会被释放(它将变成僵尸),如果我稍后尝试使用它(在块之外),程序就会崩溃。

这是我的班级中为 ios 编写的一些代码(c++ 与 Objective-C 混合)。
在块内,我尝试以两种不同的方式初始化变量 dict。

class Data
{
public:
    NSMutableDictionary *dict;

    void DoSomeStuff()
    {
    [NSSomeFrameworkTool doSomeStuffWithCompletionHandler:^(NSError *err) {
        // case 1 - OK
        dict = [[NSMutableDictionary alloc] initWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithFile:@"dict.dat"]];

        // case 2 - will crash later if i try to use dict
        dict = [NSKeyedUnarchiver unarchiveObjectWithFile:@"dict.dat"];   }];
    }
}

该类有类变量 dict,它在 DoSomeStuff() 方法中初始化。
该方法调用 ios 框架中的一个方法,该方法使用 block(作为回调)来通知我某些任务已完成。

我想知道为什么情况 1 和情况 2 的工作方式不同。也许禁止使用在该块内初始化的块外的引用?
按照案例2所示的方式这样做有什么问题吗?

I'm trying to initialize a dict variable but I don't understand why one way works, while the other does not.

In case 1 everything is alright and I can use dict later.
In case 2 it will be released very soon (It will becomes a zombie) and If I try to use it later (outside a block) the program crashes.

Here's some code from my class (c++ mixed with objective-c) written for ios.
Inside the block i tried to initialize variable dict in two different ways.

class Data
{
public:
    NSMutableDictionary *dict;

    void DoSomeStuff()
    {
    [NSSomeFrameworkTool doSomeStuffWithCompletionHandler:^(NSError *err) {
        // case 1 - OK
        dict = [[NSMutableDictionary alloc] initWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithFile:@"dict.dat"]];

        // case 2 - will crash later if i try to use dict
        dict = [NSKeyedUnarchiver unarchiveObjectWithFile:@"dict.dat"];   }];
    }
}

This class has class variable dict, which is initialized in the DoSomeStuff() method.
That method calls a method from the ios framework that uses block (as a callback) to inform me that some task is done.

I was wondering why case 1 and case 2 work different. Maybe it is forbidden to use references outside the block, that was initialized inside this block?
What's wrong with doing this the way shown in case2?

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

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

发布评论

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

评论(2

半夏半凉 2024-12-28 22:43:01

在第一种情况下,您不会释放您的字典,在第二种情况下,它是自动释放的,因此您应该保留它。

dict = [[NSKeyedUnarchiver unarchiveObjectWithFile:@"dict.dat"] retain];

In first case you don't release your dict, and in second case it is autoreleased so you should retain it.

dict = [[NSKeyedUnarchiver unarchiveObjectWithFile:@"dict.dat"] retain];
望笑 2024-12-28 22:43:01

我认为你可以在这里使用块变量。

__block NSMutableDictionary *dict;

变量在块内是不可变的。它们是常量副本,是“块创建”时变量的快照,因此不能在块内对其进行修改。块变量会将变量从“堆栈”移动到“堆”,允许您更改其状态。我绝不是块方面的专家,因为它们对 Objective c 来说相对较新。但是,如果您四处搜索,可以找到一些很好的文章来学习。

http://pragmaticstudio.com/blog/2010/7/28/ios4 -块-1

I think you can use a block variable here.

__block NSMutableDictionary *dict;

Variables are immutable inside of the block. They are a constant copy, a snapshot of the variable at the time of "block creation" so it can not be modified inside the block. The block variable will move the variable to the 'Heap' from the 'Stack' allowing you to change it's state. I'm by no means an expert on blocks, being that they are relatively new to Objective c.But there are some good articles if you google around to learn from.

http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文