私は反復的な作業を続ける関数というクラスファイルを持っています。そこにある関数の1つはGetPriceと呼ばれ、XML Webサービスに接続し、XMLを解析してCarPriceオブジェクトを返します。 CarPriceオブジェクトを返すときまで、すべてがうまく機能します。私のconnectionDidFinishLoadingでは、オブジェクトがnullではなくてもNULLです。ここでIOS 5.0 - NSURLConnectionは動作しますが、完了する前にNullを返します
は私のGetPrice機能である:MPが読み込まれる前に
-(CarPrice*)GetPrice:(NSString *)m
{
NSString *url =[@"http://myUrl.com"];
dataWebService = [NSMutableData data];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
return mp; //mp is declared as a CarPrice in the @interface section of my funcs class
//when it gets returned here it is NULL even though....(see below)
}
//Connection Functions=======================
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[dataWebService setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[dataWebService appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
ParserMetal *pmr = [ParserMetal alloc];
mp = [pmr parseMetal:responseString];
//at this point, the mp is fully populated
//an NSLOG(@"%@", mp.displayPrice); here will show that the mp object is populated
//however as stated above, when it returns, it is null.
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Error during Connection: %@", [error description]);
}
//End Connection Functions ==================
はreturn mp;
起こっているのでしょうか?返信前にデータが入力されていることを確認するには、ここで同期接続を使用する必要がありますか?
mainClassでは、GetPrice関数が呼び出されているMainViewControllerを意味しますか? イム本当にわからないことを行う方法を.... 私が持っている問題は、私はそうのような関数には、いくつかの呼び出しを行うことです: CarPrice * HP = [GetPrice FN:「ホンダ」@]。 CarPrice * tp = [fn GetPrice:@ "toyota"]; CarPrice * cp = [fn GetPrice:@ "chevrolet"]; 私は実際にアプリケーションを続ける前に、割り当てられたオブジェクトが必要です...私は 'didReceiveAllData:mp]'関数の内部にこれらのオブジェクトをどのように割り当てて、それらを別個のオブジェクト。 – user1205315
また、誰かが知っているこのようなものの実際の作業デモはありますか?チュートリアルなど。私はC#の開発者ですので、擬似コードはこの文脈では私のためには実際には機能しません。 – user1205315
と比較する例はありません。最初のコメントについて:はい。関数を定義する必要があります(例えば、 'didReceiveAllData'ですが、呼び出したいものを呼び出すことができます)。そしてそこに入ってくるすべての情報を処理します。このメソッドは、[fn GetPrice]を呼び出すクラスにある必要があります(たとえば、MainViewControllerなど)。私はそこに良いコード例があるとは思わない。しかし、Objective Cとデリゲートなどでそれほどうまくいかない場合は、同期接続に固執したいと思うかもしれませんが、これはあなたのケースで使いやすくなります。 – Robbietjuh