【发布时间】:2012-07-07 21:42:18
【问题描述】:
当我创建自定义类时,我希望能够在构建类的实例后跳过代码的 alloc init 部分。类似于它的完成方式:
NSString * ex = [NSString stringWithFormat...];
基本上我已经用自定义初始化方法设置了类来设置我的基本变量。然而,当我在前端并实际制作这些小动物时,我不得不说:
[[Monster alloc] initWithAttack:50 andDefense:45];
我更愿意说
[Monster monsterWithAttack:50 andDefense:45];
我知道去掉 alloc 部分是一件简单的愚蠢的事情,但它使代码更具可读性,所以我更喜欢这样做。我最初只是尝试从
更改我的方法-(id)initWithAttack:(int) a andDefense:(int) d
到
-(id)monsterWithAttack:(int) a andDefense:(int) d
然后将我的 self = [super init] 更改为 self = [[super alloc] init]; 但这显然不起作用!有什么想法吗?
【问题讨论】:
-
monsterWithAttack 必须是类方法,所以将 - 替换为 +
标签: objective-c ios initialization