正确的Objective-C init方法语法

时间:2022-08-05 22:33:45

Why doesn't this common property initialization scheme risk failure when the synthesized setter tries to release the undefined myArray object? Or are property objects automatically initialized to nil and I don't need to be doing this at all?

当合成的setter试图释放未定义的myArray对象时,为什么这个公共属性初始化方案不存在失败的风险?或者是属性对象自动初始化为nil,我不需要这样做?

@interface myClass : NSObject {
    NSArray* myArray;
}
@property (nonatomic, retain) NSArray* myArray;
@end

@implementation myClass
@synthesize myArray;
-(id)init {
    if ( self = [super init] ) {
        self.myArray = nil;
    }
    return self;
}

...

3 个解决方案

#1


12  

As others have stated, the instance variable is already initialised to nil.

正如其他人所说,实例变量已经被初始化为nil。

Additionally, as per Apple's documentation, instance variables should be set directly in an init method, as the getter/setter methods of a class (or subclass thereof) may rely on a fully initialised instance.

此外,根据Apple的文档,实例变量应该直接在init方法中设置,因为类(或其子类)的getter/setter方法可能依赖于完全初始化的实例。

#2


25  

Object instance variables in Objective-C are initialized to nil by default. Furthermore, messaging nil is allowed (unlike calling a method on null in function-calling languages like Java, C# or C++). The result of a message to nil is nil, this calling [nil release]; is just nil, not an exception.

Objective-C中的对象实例变量在默认情况下被初始化为nil。此外,还允许消息传递nil(不像在Java、c#或c++等函数调用语言中调用null的方法)。对nil消息的结果是nil,这个调用[nil释放];是nil,不是例外。

On a side note, it's best practice to assign/call instance variables directly in -init and -dealloc methods:

另外,最好的做法是直接在-init和-dealloc方法中分配/调用实例变量:

-(id)init {
    if ( self = [super init] ) {
        myArray = nil;
    }
    return self;
}

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

#3


3  

It's already initialized to nil.

它已经被初始化为nil。

#1


12  

As others have stated, the instance variable is already initialised to nil.

正如其他人所说,实例变量已经被初始化为nil。

Additionally, as per Apple's documentation, instance variables should be set directly in an init method, as the getter/setter methods of a class (or subclass thereof) may rely on a fully initialised instance.

此外,根据Apple的文档,实例变量应该直接在init方法中设置,因为类(或其子类)的getter/setter方法可能依赖于完全初始化的实例。

#2


25  

Object instance variables in Objective-C are initialized to nil by default. Furthermore, messaging nil is allowed (unlike calling a method on null in function-calling languages like Java, C# or C++). The result of a message to nil is nil, this calling [nil release]; is just nil, not an exception.

Objective-C中的对象实例变量在默认情况下被初始化为nil。此外,还允许消息传递nil(不像在Java、c#或c++等函数调用语言中调用null的方法)。对nil消息的结果是nil,这个调用[nil释放];是nil,不是例外。

On a side note, it's best practice to assign/call instance variables directly in -init and -dealloc methods:

另外,最好的做法是直接在-init和-dealloc方法中分配/调用实例变量:

-(id)init {
    if ( self = [super init] ) {
        myArray = nil;
    }
    return self;
}

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

#3


3  

It's already initialized to nil.

它已经被初始化为nil。