简体   繁体   English

Objective-c类的默认init方法?

[英]objective-c default init method for class?

I have two differing methods for initializing my objective-c class. 我有两种不同的方法来初始化我的objective-c类。 One is the default, and one takes a configuration parameter. 一个是默认值,一个是配置参数。 Now, I'm pretty green when it comes to objective-c, but I've implemented these methods and I'm wondering if there's a better (more correct/in good style) way to handle initialization than the way I have done it. 现在,当谈到objective-c时我很绿,但是我已经实现了这些方法,我想知道是否有更好的(更正确/更好的风格)方式来处理初始化而不是我的方式。 Meaning, did I write these initialization functions in accordance with standards and good style? 意思是,我是否按照标准和良好的风格编写了这些初始化函数? It just doesn't feel right to check for the existence of selfPtr and then return based on that. 检查selfPtr是否存在然后基于此返回是不正确的。

Below are my class header and implementation files. 下面是我的类头和实现文件。 Also, if you spot anything else that is wrong or evil, please let me know. 此外,如果你发现任何其他错误或邪恶的东西,请告诉我。 I am a C++/Javascript developer who is learning objective-c as hobby and would appreciate any tips that you could offer. 我是一名C ++ / Javascript开发人员,他正在学习Objective-c作为业余爱好,并感谢您提供的任何提示。

#import <Cocoa/Cocoa.h>

// class for raising events and parsing returned directives

@interface awesome : NSObject {
 // silence is golden. Actually properties are golden. Hence this emptiness.
}

// properties
@property (retain) SBJsonParser* parser;
@property (retain) NSString* eventDomain;
@property (retain) NSString* appid

// constructors
-(id) init;
-(id) initWithAppId:(id) input;

// destructor
-(void) dealloc;


@end

#import "awesome.h"
#import "JSON.h"


@implementation awesome



- (id) init {
 if (self = [super init]) {
  // if init is called directly, just pass nil to AppId contructor variant
  id selfPtr = [self initWithAppId:nil];
 }

 if (selfPtr) {
  return selfPtr;
 } else {
  return self;
 }
}

- (id) initWithAppId:(id) input {
 if (self = [super init]) {
  if (input = nil) {
   input = [[NSString alloc] initWithString:@"a369x123"];
  }
  [self setAppid:input];
  [self setEventDomain:[[NSString alloc] initWithString:@"desktop"]];
 }
 return self;
}

// property synthesis
@synthesize parser;
@synthesize appid;
@synthesize eventDomain;

// destructor
- (void) dealloc {
 self.parser = nil;
 self.appid = nil;
 self.eventDomain = nil;
 [super dealloc];
}

@end

Thanks! 谢谢!

When one initializer simply performs the more complex initializer with some default parameters, call it as such: 当一个初始化程序只使用一些默认参数执行更复杂的初始化程序时,请将其调用为:

-(id)init {
  return [self initWithAppID:nil];
}

-(id)initWithAppID:(id)input {
  if (self = [super init]) {
    /* perform your post-initialization logic here */
  }
  return self;
}

Usually you try to make one of the initializers the "designated initializer", meaning it's the one that always gets invoked. 通常,您尝试将其中一个初始化程序设为“指定的初始化程序”,这意味着它始终被调用。 In this case that's -initWithAppID: . 在这种情况下,是-initWithAppID: .

Your init method should call the preferred initializer, initWithAppId:, instead of the super implementation. 您的init方法应该调用首选初始化程序initWithAppId:而不是超级实现。 Then the initWithAppId calls the super implementation, as it does. 然后initWithAppId调用超级实现,就像它一样。 Also, in initWithAppId:, you have if(input = nil), which will always set input to nil and evaluate to YES. 另外,在initWithAppId:中,你有if(input = nil),它总是将输入设置为nil并计算为YES。 Here are the proper implementations. 这是正确的实现。

- (id)init {
    return [self initWithAppId:nil];
}
- (id)initWithAppId:(id)input {
    if((self = [super init])) {
        if(input == nil) input = @"a369x123";
        self.appid = input;
        self.eventDomain = @"desktop";
    }
    return self;
}

To be honest, I see this as a moot point. 说实话,我认为这是一个有争议的问题。 Your second initialization method makes no sense when receiving a nil argument (plus you have a logic problem in your conditional checking if input is nil). 接收nil参数时,第二个初始化方法没有任何意义(如果输入为nil,则在条件检查中存在逻辑问题)。 What I would do in this case, is provide one initialization method, and two factory class methods which act in the usual way: Return autoreleased instances, and in one of them, provide your default value. 在这种情况下我要做的是提供一种初始化方法,以及两种以通常方式工作的工厂类方法:返回自动释放的实例,并在其中一种中提供默认值。

For instance, declare a class method: 例如,声明一个类方法:

+ (awesome*)awesome;
+ (awesome*)awesomeWithAppId:(id)foo;

and in your implementation for +awesome for instance, write it like this: 例如,在你的+awesome实现中,这样写:

+ (awesome*)awesome
{
    return [[[awesome alloc] initWithAppId:@"a369x123"] autorelease];
}

And likewise, in your awesomeWithAppId: something like this: 同样,在你的awesomeWithAppId:这样的事情:

+ (awesome*)awesomeWithAppId:(id)foo
{
    return [[[awesome alloc] initWithAppId:foo] autorelease];
}

Then again, this may just be me. 然后,这可能只是我。

The default will be whichever one you choose to call, default值为您选择的任何一个,

[awesome alloc] init];
[awesome alloc] initWithAppId:ID];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM