构造函数 [英] constructor in objective c
问题描述
HI,
我创建了我的iPhone应用程序,但我有一个问题。
我有一个 classViewController
其中我实现了我的程序。
我必须alloc 3 NSMutableArray
但我不想做在grapich方法。
我的类没有像java这样的构造函数?
非常抱歉我的英语XP
//我想把它放在类似于构造函数的方法java
arrayPosition = [[NSMutableArray alloc] init];
currentPositionName = [NSString stringWithFormat:@noPosition];
它叫做 -init
,它有点像这样:
- (id)init {
self = [super init];
if(self!= nil){
//初始化到这里。
}
return self;
}
编辑:不要忘记 -dealloc
,tho'。
- (void)dealloc {
// release owned objects here
[super dealloc]; //很重要。另外,在代码中使用本地语言通常是一个坏的举动,你通常想坚持英语,特别是当在网上寻求帮助等。
HI,
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?
Thanks so much and sorry for my english XP
// I want put it in a method like constructor java
arrayPosition = [[NSMutableArray alloc] init];
currentPositionName = [NSString stringWithFormat:@"noPosition"];
解决方案 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;
}
Edit: Don't forget -dealloc
, tho'.
- (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.
这篇关于构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!