简体   繁体   English

何时在Objective-C的初始化方法中使用self.property,如何在初始化方法中初始化数组

[英]when to use self.property in init methods in objective-c, how to initialize arrays in init methods

I'm confused on what the proper way to write custom init methods for youw own subclass in terms of memory management, custom subclasses, and arrays. 我对于在内存管理,自定义子类和数组方面为自己的子类编写自定义init方法的正确方法感到困惑。 If I have properties like: 如果我有以下属性:

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) SomeSubclassOfNSObject *object;

@interface SomeSubclassofNSObject 
@property (nonatomic, retain) NSString *category;

How do I write my init method? 如何编写我的初始化方法?

do you do: 你会做:

initWithName:(NSString *)aName object:(SomeSubclassOfNSObject *)anObject {
    if (self = [super init]) {
       self.name = aName; // or do you do name = aName or name = [aName copy] autorelease] or name = [NSString alloc] initWithFormat:@"%@", aName]
       self.object = anObject; // do I need to make a copy of this object so they don't point to the same object?

    // loop through NSMutableArray and copy the objects?
    // not really sure what to do for arrays.      
    }
    return self;
}
  1. I would recommend self.name = aName; 我会推荐self.name = aName; in most cases because this takes advantage of the generated setter and thus the retain count is incremented implicitly. 在大多数情况下,因为这会利用生成的setter的优势,因此保留计数会隐式增加。 That means your reference is safe whatever the caller of init will do afterwards with aName . 这意味着无论init的调用者随后使用aName做什么,您的引用都是安全的。 The same applies to the dealloc counterpart: just write self.name = nil; 对于dealloc对应对象也是如此:只需写成self.name = nil; and your done. 和你完成。

    Things are different if you are supplying NSMutableString because changes inside of your class will affect other classes' references to this. 如果您提供NSMutableString情况会有所不同,因为类内部的更改将影响其他类对此的引用。 But from design view mutable strings should be used as paramters only if it's your intention that they are manipulated. 但是从设计的角度来看,仅当您希望操纵可变字符串时,才应将可变字符串用作参数。

  2. self.object = anObject; : Well it depends what you want. :嗯,这取决于您想要什么。 If all classes should point to the same object don't make copy (I think this is most often exactly what you want). 如果所有类都应指向同一个对象,则不要进行复制(我认为这通常正是您想要的)。 There might be cases when it is reasonable to make a deep copy. 在某些情况下,进行深复制是合理的。

  3. Arrays: You need something like array = [[NSMutableArray alloc] init]; 数组:您需要类似array = [[NSMutableArray alloc] init]; in your init method. 在您的init方法中。 Afterwards the retain count will be incremtented automatically for every object you add ie you must not call retain on the added object itself. 之后,对于您添加的每个对象,保留计数将自动增加,即您不得在添加的对象本身上调用保留。 Like in 2. there might be situations where you really want to have copies of the objects of some source array. 像在2中一样。在某些情况下,您可能确实希望拥有某个源数组的对象的副本。 OK do it but this is rarely the case. 可以,但是很少有这种情况。

    More often you have an existing array and want a sub-array of it or a special sorted version. 通常,您有一个现有的数组,并且想要它的子数组或特殊排序的版本。 So you have got a new array but the same elements. 因此,您有了一个新数组,但元素相同。

This was the short answer. 这是简短的答案。 More about this in: 有关更多信息,请参见:

Advanced Memory Management Programming Guide 高级内存管理编程指南

Declared Properties 申报物业

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

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