简体   繁体   English

在Objective-C中分配属性

[英]Allocating a property in Objective-C

I have the following questions about allocating properties in Objective-C, 关于在Objective-C中分配属性,我有以下问题,

if I have the following property: 如果我具有以下属性:

@property (nonatomic, retain) NSArray *arr;
@synthize arr=_arr;

should I allocate this array like: 我应该像这样分配这个数组吗?

arr=[[NSArray alloc]init];

and if I should to, where is the best place to to allocate it? 如果应该的话,分配它的最佳位置在哪里?

I know I should release it, but retaining a property will increase its retain count by 1 and allocating it will increase it by another 1, am I right. 我知道我应该释放它,但是保留一个属性会使它的保留计数增加1,而分配它会使它的保留计数又增加1,对吗。

You can allocate the array in two ways: 您可以通过两种方式分配数组:

  1. Set the ivar directly with a retained value, like this: 直接使用保留值设置ivar,如下所示:

     _arr = [[NSArray alloc] init]; 
  2. Set the property with an autoreleased value like this: 使用如下所示的自动释放值设置属性:

     self.arr = [NSArray array]; 

You can do this in your class's init method, although you've not said what superclass this is using so I'm not sure how the init method should look. 您可以在您的类的init方法中执行此操作,尽管您没有说这使用的是超类,所以我不确定init方法的外观。 If it's an NSObject, it will look like this: 如果是NSObject,它将如下所示:

- (id)init
{
    if ((self = [super init]))
    {
         self.arr = [NSArray array];
    }
    return self;
}

But if it's a UIViewController, you'll need to use initWithNibName:bundle, like this: 但如果是UIViewController,则需要使用initWithNibName:bundle,如下所示:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)bundleOrNil
{
    if ((self = [super itWithNibName:nibNameOrNil bundle:bundleOrNil]))
    {
         self.arr = [NSArray array];
    }
    return self;
}

Then you need to release it in your dealloc method, like this: 然后,您需要在dealloc方法中释放它,如下所示:

- (void)dealloc
{
    [_arr release];
    [super dealloc];
}

However, you should bear in mind that NSArrays can't be changed once they're created, so you probably either want to initialise it with some objects, like this: 但是,请记住,一旦创建NSArray,就无法再对其进行更改,因此您可能要使用某些对象来初始化它,如下所示:

self.arr = [NSArray arrayWithObjects:object1, object2, nil];

Or if you want to add objects to it later, you should define it as an NSMutableArray, like this: 或者,如果您想在以后添加对象,则应将其定义为NSMutableArray,如下所示:

@property (nonatomic, retain) NSMutableArray *arr;

And init it with: 并使用以下命令初始化它:

self.arr = [NSMutableArray array];

That will let you add objects to it later. 这样一来,您以后便可以向其中添加对象。

By the way, if you get errors in your dealloc method, it probably means that your project is using ARC, which is a new technology in iOS 5 that means you don't need to write release and retain statements any more. 顺便说一句,如果您在dealloc方法中遇到错误,则可能意味着您的项目正在使用ARC,这是iOS 5中的一项新技术,这意味着您不再需要编写发布和保留语句。

If you aren't using ARC I suggest you do so because it will save you having to worry about this retain/release stuff - any iOS developer who is just starting out now should really use ARC for every project, but because it was only introduced in iOS 5, a lot of the Objective-C books don't cover it. 如果您不使用ARC,我建议您这样做,因为这样可以避免您担心保留/发布的问题-现在刚开始使用的所有iOS开发人员都应该对每个项目都真正使用ARC,但是因为它只是被引入的在iOS 5中,很多Objective-C书籍都没有介绍它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM