Objective-C的类的默认初始化方法? [英] objective-c default init method for class?

查看:165
本文介绍了Objective-C的类的默认初始化方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种不同的方法来初始化我的Objective-C类.一个是默认值,一个带有配置参数.现在,对于Objective-C来说,我还是很环保的,但是我已经实现了这些方法,我想知道是否有比我做的方法更好的(更正确/更好的样式)处理初始化的方法.意思是,我是否按照标准和良好的风格编写了这些初始化函数?只是不正​​确地检查selfPtr是否存在,然后基于此返回.

I have two differing methods for initializing my objective-c class. 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. 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.

下面是我的类头文件和实现文件.另外,如果您发现任何其他错误或邪恶的内容,请告诉我.我是一名C ++/Javascript开发人员,正在将Objective-c作为业余爱好学习,并希望您能提供任何提示.

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.

#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;
}

通常,您尝试将一个初始化程序设为指定的初始化程序",这意味着它总是被调用.在这种情况下是-initWithAppID:.

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:.

这篇关于Objective-C的类的默认初始化方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆