我有一个关于在objective-c类中创建多个初始化器的简单问题。基本上,我有一个类来表示我的数据库(users)中的一行。我目前有一个初始化器,它初始化基于用户UserID (这也是数据库中的主键)的类,当传递给UserID时,类将连接到key服务,解析结果,并返回初始化到数据库中相应行的对象。
在这个数据库中有许多唯一的字段(用户名和电子邮件地址),我也希望能够根据这些值初始化我的对象。但我不确定如何拥有多个初始化器,我读到的所有东西都表明我可以自由拥有多个初始化器,只要每个初始化器都调用指定的初始化器。如果有人能帮我解决这个问题,那就太好了。
我的初始化代码如下:
- (id) initWithUserID:(NSInteger) candidate {
self = [super init];
if(self) {
// Load User Data Here
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<GetByUserID xmlns=\"http://tempuri.org/\">\n"
"<UserID>%d</UserID>\n"
"</GetByUserID>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", candidate
];
NSLog(@"%@",soapMessage);
// Build Our Request
NSURL *url = [NSURL URLWithString:@"http://photoswapper.mick-walker.co.uk/UsersService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/GetByUserID" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSError *WSerror;
NSURLResponse *WSresponse;
webData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&WSresponse error:&WSerror];
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
}
return self;
}根据Laurent的评论,我已经尝试实现我自己的解决方案,如果您能告诉我这个解决方案的任何明显的陷阱,我将不胜感激:
我不能完全理解你的意思,我已经尝试实现了我自己的解决方案。如果你能让我知道你的想法,我将不胜感激:
- (id) init {
self = [super init];
if(self){
// For simplicity I am going to assume that the 3 possible
// initialation vectors are mutually exclusive.
// i.e if userName is used, then userID and emailAddress
// will always be nil
if(self.userName != nil){
// Initialise object based on username
}
if(self.emailAddress != nil){
// Initialise object based on emailAddress
}
if(self.userID != 0){ // UserID is an NSInteger Type
// Initialise object based on userID
}
}
return self;
}
- (id) initWithUserID:(NSInteger) candidate {
self.userID = candidate;
return [self init];
}
- (id) initWithEmailAddress:(NSString *) candidate {
self.emailAddress = candidate;
return [self init];
}
- (id) initWithUserName:(NSString *) candidate {
self.userName = candidate;
return [self init];
}问候
发布于 2010-01-08 18:48:54
指定的初始化器的定义是here。无论您使用哪种初始化器,都必须确保实例的一致性。要获得完整的参考,请参阅Objective-C Programming Guide:初始化器实现有很好的文档记录。
编辑2:修复@NSResponder报告的拼写错误
编辑:
我认为在设置成员后调用init是不可靠的。成员可能有一些奇怪的值,这些值将无法通过初始化测试。
更好的方法是首先调用"init“方法(它将为成员设置默认值),然后设置成员。这样,您的所有初始化器都具有相同的代码结构:
- (id) init {
self = [super init];
if(self){
self.userID = 0;
self.userName = nil;
self.emailAddress = nil;
}
return self;
}
- (id) initWithUserID:(NSInteger) candidate {
self = [self init];
if(self){
self.userID = candidate;
}
return self;
}发布于 2010-01-08 22:33:23
我倾向于这样做的方式是,指定的初始化器是接受参数最多的方法,而更简单的初始化器只发送更详细的-init消息。例如:
// simple initializer
- (id) init
{
return [self initWithWidth: 1.0];
}
// not so simple initializer
- (id) initWithWidth:(float) aWidth
{
return [self initWithWidth:aWidth andColor:nil];
}
// designated initializer. This is the one that subclasses should override.
- (id) initWithWidth: (float) aWidth andColor: (NSColor *) aColor
{
if (self = [super init])
{
self.width = aWidth;
self.color = aColor ? aColor : [[self class] defaultColor];
}
return self;
}https://stackoverflow.com/questions/2026914
复制相似问题