初始化块内的对象
我正在尝试初始化一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一种情况下,您不会释放您的字典,在第二种情况下,它是自动释放的,因此您应该保留它。
In first case you don't release your dict, and in second case it is autoreleased so you should retain it.
我认为你可以在这里使用块变量。
变量在块内是不可变的。它们是常量副本,是“块创建”时变量的快照,因此不能在块内对其进行修改。块变量会将变量从“堆栈”移动到“堆”,允许您更改其状态。我绝不是块方面的专家,因为它们对 Objective c 来说相对较新。但是,如果您四处搜索,可以找到一些很好的文章来学习。
http://pragmaticstudio.com/blog/2010/7/28/ios4 -块-1
I think you can use a block variable here.
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