目标C的基础 [英] Basic of Objective C

查看:76
本文介绍了目标C的基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@implementation GroupedInexedViewController
{
    NSDictionary *names;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
                                                 ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    names = dict;
    [dict release];
}

解除分配 'dict' 会影响 'names' 吗?我的意思是它也解除分配名称"吗?我在另一篇文章中看到这是一种不好的做法?但为什么?

Is deallocating 'dict' affects 'names'? I mean does it deallocate 'names' too? I saw in another post that its a bad practice? But why?

ARC 已禁用.

推荐答案

简而言之,如果您不使用 ARC,是的:取消分配 dict 将影响 names.这是因为您将 names 指针分配给您已分配的单个 NSDictionary.

In short, if you are not using ARC, yes: deallocating dict will affect names. This is because you are assigning the names pointer to the single NSDictionary you have allocated.

如果你想让 names 在你处理 dict 时保留 NSDictionary,你需要发送 dict 一条 retain 消息:

If you wanted to have names retain the NSDictionary when you dealloc dict, you would need to send dict a retain message:

names = [dict retain];

这篇关于目标C的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆