NSObject によって継承された別のクラスで post メソッドを作成しています。そこでは、ユーザー名とパスワード、およびデリゲートとしてのビューコントローラーの参照という 2 つのパラメーターを送信しています。このクラスのイニシャライザ メソッドをどのように構築すればよいでしょうか。また、通常、ネイティブ パーサーを使用してこのクラスで解析を実行します。
これまでのところ、私はこれを行ってきました:
-(void)initWith:(NSString *)username password:(NSString*)password delegate:(id<name>)delegate
{
NSString *soapMessage = [[NSString alloc] initWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<soapenv:Envelope \n"
"xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
"xmlns:tem=\"http://tempuri.org/\"> \n"
"<soapenv:Header/>\n"
"<soapenv:Body>\n"
"<tem:Login>\n"
"<tem:username>%@</tem:username>\n"
"<tem:password>%@</tem:password>\n"
"</tem:Login>\n"
"</soapenv:Body>\n"
"</soapenv:Envelope>\n",hisUDID,myUDID];
NSURL *url = [NSURL URLWithString:kBaseURL];
// NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *msgLength = [[NSString alloc] initWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/XXXXX/Login" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[msgLength release];
[soapMessage release];
[theRequest release];
theRequest=nil;
if(theConnection)
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"The Connection is NULL");
}
}
一般に、イニシャライザが戻り型 ID を持ち、必要な sata 構造を初期化するのを見てきました... init メソッドを構築する良い方法を学びたいので、アドバイスしてください!!