objective-c 添加自定义initWith?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1711222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a custom initWith?
提问by fuzzygoat
If I create a custom initWith for an object do I essentially include the code I would add should I want to override init?
如果我为一个对象创建一个自定义的 initWith ,我是否基本上包含了我想覆盖 init 时要添加的代码?
-(id) init {
self = [super init];
if (self) {
NSLog(@"_init: %@", self);
}
return(self);
}
e.g.
例如
-(id) initWithX:(int) inPosX andY:(int) inPosY {
self = [super init];
if(self) {
NSLog(@"_init: %@", self);
posX = inPosX;
posY = inPosY;
}
return(self);
}
gary
加里
回答by stefanB
You can create one designatedinitializer that accepts all parameters that you want to make available in initialization.
您可以创建一个指定的初始化程序,它接受您希望在初始化时可用的所有参数。
Then you call from your other -(id)inityour designated initializer with proper parameters.
然后,您-(id)init使用适当的参数从其他指定的初始化程序调用。
Only the designated initializer will initialize super class [super init].
只有指定的初始化器才会初始化超类[super init]。
Example:
例子:
- (id)init
{
return [self initWithX:defaultX andY:defaultY];
}
- (id)initWithPosition:(NSPoint)position
{
return [self initWithX:position.x andY:position.y];
}
- (id)initWithX:(int)inPosX andY:(int)inPosY
{
self = [super init];
if(self) {
NSLog(@"_init: %@", self);
posX = inPosX;
posY = inPosY;
}
return self;
}
The designated initializer is -(id)initWithX:andY:and you call it from other initializers.
指定的初始化程序是-(id)initWithX:andY:,您可以从其他初始化程序调用它。
In case you want to extend this class you call your designated initializer from subclass.
如果你想扩展这个类,你可以从子类调用指定的初始化程序。
回答by dbachrach
I'd suggest creating one main initializer that handles most of the work. You can then create any number of other initializers that all call this main one. The advantage of this is if you want to change the initialization process, you'll only have to change one spot. It might look like this:
我建议创建一个处理大部分工作的主初始化程序。然后,您可以创建任意数量的其他初始化程序,这些初始化程序都称为主初始化程序。这样做的好处是如果你想改变初始化过程,你只需要改变一个地方。它可能看起来像这样:
-(id) initWithX:(float)x {
if (self = [super init]) {
/* do most of initialization */
self.xVal = x;
}
return self;
}
-(id) init {
return [self initWithX:0.0f];
}
In this example initWithX: is our main initializer. The other initializer (init) simply calls initWithX: with a default value (in this case 0).
在这个例子中 initWithX: 是我们的主要初始化器。另一个初始值设定项 (init) 只是使用默认值(在本例中为 0)调用 initWithX:。
回答by Benjamin Cox
Yes, that's exactly how I do it. One slight change will cut out a line of code:
是的,这正是我的做法。稍微改动一下就会删掉一行代码:
if (self = [super init]) {
As opposed to:
与之相反:
self = [super init];
if(self) {
回答by ohho
For modern Objective-C...
对于现代Objective-C...
UDFile.h
UD文件
#import <Foundation/Foundation.h>
@interface UDFile : NSObject
@property (nonatomic, strong) NSString *name;
- (instancetype)initWithName:(NSString *)name NS_DESIGNATED_INITIALIZER;
@end
UDFile.m
UD文件
#import "UDFile.h"
@implementation UDFile
- (instancetype)initWithName:(NSString *)name {
self = [super init];
if (self) {
_name = [name copy];
}
return self;
}
- (instancetype)init {
return [self initWithPathname:@""];
}
回答by dreamlax
Sometimes, you want to reuse some initialisation code and modify the behaviour only slightly for specific initialisers. In this case, I do the following:
有时,您想重用一些初始化代码并仅针对特定的初始化程序稍微修改行为。在这种情况下,我执行以下操作:
- (id) init
{
self = [super init];
if (!self) return nil;
// These values are always initialised this way
ivar1 = 10;
ivar2 = @"HellO";
ivar3 = [[NSMutableArray alloc] initWithCapacity:10];
ivar4 = 22;
return self;
}
- (id) initWithIvar4:(int) aValue
{
// call -init on self, which will call -init on super for us, and set
// up ivar1, ivar2, ivar3, and ivar4.
self = [self init];
if (!self) return nil;
// Change ivar4 from the default 22 to whatever aValue is.
ivar4 = aValue;
return self;
}

