我有一个关于在objective-c类中创建多个初始化器的简单问题.基本上我有一个代表我的数据库(用户)中的单行的类.我现在有这初始化基于用户类的初始化器用户ID(这也是数据库中的主键),当通过用户ID的类将它们连接到Web服务解析结果并返回初始化为相应的行对象在数据库中.
在这个数据库中有许多独特的字段(用户名和电子邮件地址),我也希望能够根据这些值初始化我的对象.但我不确定如何使用多个初始化程序,我读过的所有内容都表明我可以自由拥有多个初始化程序,只要每个程序都调用指定的初始化程序.如果有人可以帮我解决这个问题,那就太好了.
我的初始化代码如下:
- (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 …Run Code Online (Sandbox Code Playgroud)