Objective-C中的构造方法 [英] Constructor in Objective-C
问题描述
我已经创建了我的iPhone应用程序,但是有问题。
我有一个 classViewController
在其中实现了我的程序。
我必须分配3个 NSMutableArray
,但是我不想在grapich方法中这样做。
我的课程中没有像Java这样的构造函数吗?
I have created my iPhone apps but I have a problem.
I have a classViewController
where I have implemented my program.
I must alloc 3 NSMutableArray
but I don't want do it in grapich methods.
There isn't a constructor like Java for my class?
// I want put it in a method like constructor java
arrayPosition = [[NSMutableArray alloc] init];
currentPositionName = [NSString stringWithFormat:@"noPosition"];
推荐答案
是的,有一个初始化程序。它叫做 -init
,它有点像这样:
Yes, there is an initializer. It's called -init
, and it goes a little something like this:
- (id) init {
self = [super init];
if (self != nil) {
// initializations go here.
}
return self;
}
编辑:不要忘记 -dealloc
,。
- (void)dealloc {
// release owned objects here
[super dealloc]; // pretty important.
}
请注意,在代码中使用本地语言通常是不好的举动,您通常希望坚持使用英语,尤其是在在线寻求帮助时。
As a side note, using native language in code is generally a bad move, you usually want to stick to English, particularly when asking for help online and the like.
这篇关于Objective-C中的构造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!