2013-10-01 20 views
0

我對與塊客觀C編程有點困惑。如何使用此塊方法?

例如下面是一個方法:

在.H

- (void)downloadDataWithURLString:(NSString *)urlString 
       completionHandler:(void(^) (NSArray * response, NSError *error))completionHandler; 
在.M

- (void)downloadedDataURLString:(NSString *)urlString 
       completionHandler:(void (^)(NSArray *, NSError *))completionHandler { 
// some things get done here. But what!? 
} 

我的主要問題是....?如何實施此完成處理程序?數組和錯誤會返回哪些變量?它是代碼的一個區域,但是如何告訴它在完成時應該怎麼做?

+2

你不能確定從塊簽名本身,當然沒有給出參數名稱。你必須閱讀文檔或一些示例代碼。我們無法幫助。 – trojanfoe

+0

我用更多的代碼更新了它。 – jimbob

+0

嗯,我們可以猜測它做了什麼,但它只是一個猜測。就像任何方法一樣,例如,我向您提供了它,它做了什麼以及如何正確使用它是通過文檔和/或示例進行交流的。有多種方法可以實現同樣的目標。你需要參考代碼的作者,或者至少發佈一個鏈接供我們關注。 – trojanfoe

回答

1

這是給調用者提供的代碼通過方法(塊體)上運行。實現者需要調用該代碼。

要開始一個簡單的例子,比如說來電者只是想讓你形成與urlString數組和回調,那麼你可以這樣做:

- (void)downloadedDataURLString:(NSString *)urlString 
       completionHandler:(void (^)(NSArray *, NSError *))completionHandler { 

    NSArray *callBackWithThis = @[urlString, @"Look ma, no hands"]; 
    completionHandler(callBackWithThis, nil); 
} 

主叫方這樣做:

- (void)someMethodInTheSameClass { 

    // make an array 
    [self downloadedDataURLString:@"put me in an array" 
        completionHandler:^(NSArray *array, NSError *error) { 
     NSLog(@"called back with %@", array); 
    }]; 
} 

來電者將記錄兩個項目陣列@「把我的數組」,並@「媽媽快看,沒有動手。」在一個更現實的例子中,假設有人要求你在完成下載任務時給他們回電話:

- (void)downloadedDataURLString:(NSString *)urlString 
       completionHandler:(void (^)(NSArray *, NSError *))completionHandler { 
    // imagine your caller wants you to do a GET from a web api 
    // stripped down, that would look like this 

    // build a request 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    // run it asynch 
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

     if (!error) { 
      // imagine that the api answers a JSON array. parse it 
      NSError *parseError; 
      id parse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError]; 

      // here's the part you care about: the completionHandler can be called like a function. the code the caller supplies will be run 
      if (!parseError) { 
       completionHandler(parse, nil); 
      } else { 
       NSLog(@"json parse error, error is %@", parseError); 
       completionHandler(nil, parseError); 
      } 
     } else { 
      NSLog(@"error making request %@", error); 
      completionHandler(nil, error); 
     } 
    }]; 

    // remember, this launches the request and returns right away 
    // you are calling the block later, after the request has finished 
} 
+0

我不這麼認爲; OP在詢問如何調用該方法,但未實施。 – trojanfoe

+0

@trojanfoe,問題說:「我該如何實現完成處理」。在方法內部評論說「//在這裏完成什麼事情」等等。但是我在談論區塊時會發現它很混亂。將編輯。 – danh

+0

這就是我正在尋找的。直到你展示了這個例子,謝謝你!我對這個塊是如何有用的完全感到困惑。 – jimbob

0

雖然我不能沒有看到有關的方法,或者其具體實現的更多細節完全肯定我懷疑這一點:這個方法創建一個新的後臺線程,從服務器中檢索數據和JSON/XML轉換成一個NSArrayresponse。如果發生錯誤,則error對象包含一個指向NSError。完成後,完成處理程序在主線程上調用。完成處理程序是您可以指定在嘗試檢索數據後應執行哪個代碼的塊。
這裏是如何調用此方法來獲取一些樣本代碼,您開始:

[self downloadDataWithURLString:@"http://www.google.com" 
       completionHandler:^(NSArray *response, NSError *error) { 
        if (! error) { 
         // Do something awesome with the 'response' array 
        } else { 
         NSLog(@"An error occured while downloading data: %@", error); 
        } 

}];