- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我是 Objective-C 的新手,我有一个问题。
我创建了一个自定义类并尝试为初始化创建重载:
- (id)init
{
if (self = [super init]) {
[self setIsCurrentCar:NO];
}
return self;
}
-(id) initWithID:(NSInteger)id {
if(self = [self init]) {
[self setID:id];
}
return self;
}
-(id) initWithID:(NSInteger)id CarYear:(NSString *)year {
if(self = [self initWithID:id]) {
[self setCarYear:year];
}
return self;
}
假设我在某一时刻调用了 -(id) initWithIDCarYear
方法。
我想知道上面的代码在结构上是否正确。
self
被设置了 3 次。有没有更好的解决方案? if(self = ...)
还是它是一个冗余代码?谢谢
@编辑下面的代码更好吗?
-(id) initWithID:(NSInteger)id CarYear:(NSString *)year {
if (self = [super init]) {
[self setIsCurrentCar:NO];
[self setID:id];
[self setCarYear:year];
}
return self;
}
最佳答案
虽然您的代码没问题,但我会以相反的顺序构建初始化调用,其中最详细的是指定的初始化器,而更通用的是一些默认值:
-(id) initWithID:(NSInteger)id
CarYear:(NSString *)year
{
if(self = [super init]) {
_year = year;
_id = id;
}
return self;
}
-(id)initWithID:(NSInteger)id
{
return [self initWithID:id CarYear:@"unset"];
}
-(id)init
{
return [self initWithID:0];
}
如果调用更通用的初始化程序之一会生成非法状态,您可以改为抛出错误以禁止使用它。
假设,汽车需要有 ID,但不需要年份。使用initWithID
是可以的,但是使用init
会导致状态不一致,所以我们想强制不要使用它:
-(id)init
{
[NSException raise:NSInternalInconsistencyException
format:@"You must use -initWithID: or -initWithID:CarYear:", NSStringFromSelector(_cmd)];
return nil;
}
- In this code, self is set for 3 times. Is there a better solution?
见上文
- Do I have memory leak in this code? (using ARC)
没有,一切都很好
- Do I have to check for if(self = ...) always or it is a redundant code?
正如我向您展示的:您可以在一个链中调用不同的 init 方法。只有该链中的最后一个需要执行该操作。
-(id) initWithID:(NSInteger)id CarYear:(NSString *)year {
if (self = [super init]) {
[self setIsCurrentCar:NO];
[self setID:id];
[self setCarYear:year];
}
return self;
}
你不应该在初始化方法中对自己使用 setter,see Apple's docs .
关于ios - 自定义类的 Objective-C 多重嵌套 initWith,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995718/
我有一个 objective-c 类 (RDAlertView),它创建一个警报(iOS =8 的 UIAlertController) 这是 Objective-C 中的代码: RDAlertVie
例如,我有一个 UIView 的实现。 UIView 包含两个标签,一个图像和一个框架。 我的“init”方法最终看起来像这样: - (id)initWithFrameAndLabelArrayAnd
我正在学习 ObjC 和 Cocoa,这也是我第一次认真尝试一般编程。 我无法区分在实例上调用的 initWith 方法和在类上调用的工厂方法。 首先,为什么将它们称为“工厂”方法,我称为“InitW
如果我为一个对象创建一个自定义 initWith,我是否本质上包含了我想要覆盖 init 时要添加的代码? -(id) init { self = [super init]; if (
我正在创建一个 barbutton 项并将其添加到我的导航 Controller 工具栏 UIBarButtonItem *barButtonItem = [[[UIBarButtonItem all
关于如何确保 View Controller 的 viewDidLoad 上的 outlet 为 nil,我学到了一个非常痛苦的教训,那就是通过编写您自己的 initWith... 方法。 -(id)
例如: 在界面中: @property(retain) NSObject* anObject; 在界面中,实现中: -(id)initWithAnotherObject:(NSObject*)anot
过去,我们受过这样的训练来编写 init 方法: Foo.h: -(id) initWithInteger: (NSInteger) someNumber; Foo.m: -(id) initWith
我是 Objective-C 的新手,我有一个问题。 我创建了一个自定义类并尝试为初始化创建重载: - (id)init { if (self = [super init]) {
创建数组(或使用其各自工厂方法的任何其他集合)的优缺点是什么 [[NSArray alloc] init] 对比 [NSArray array] 在 Objective-C 中?似乎后一种工厂方法让我
我的 Swift-Class 通过以下代码定义了一个 initWith.. 方法 convenience init(controlType: ApplePencilControlType) {
我已经用 Objective-C 编码几个月了,我注意到有时会使用 init 方法实例化一个类(按照文档的建议)。因此,必须先分配,然后初始化。 [[类分配示例] initWithProperty1:
应用程序在初始化 AKMicrohone 时崩溃。 Objective-C AudioKit :4.8 -(void)setupFrequencyDetector{ @try {
我是一名优秀的程序员,十分优秀!