2017-03-08 40 views
1

我在類中創建一個方法。完成處理程序在目標c中的方法

- (void)getTableData:(NSString *)URL withCompletionHandler:(void (^)(NSString *))handler{ 

__block NSDictionary *JSON; 
[manager POST:urlString parameters:jsonDict success:^(AFHTTPRequestOperation *operation, id responseObject){ 

    JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error]; 
    handler(JSON); 
} 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"error %@",error); 
      // handle failure 
     }]; 
} 

和由

[ObjOfSecondClass getTableData:BILL withCompletionHandler:^(NSString* returnString)handler{ 
    }]; 

調用它在另一個類中它示出了在處理預期表達錯誤。

+0

你想在你的回調中做什麼? – iPeter

+0

NSDictionary * JSON; – Lenin

+0

我還沒有真正理解你的問題。你可以請更具體嗎? – iPeter

回答

2

這是表達式錯誤,因爲您以錯誤的方式使用它。

試試這個在viewDidLoad

[ObjOfSecondClass getTableData:BILL withCompletionHandler:^(NSString* returnString){ 
}]; 

handler則採用的是模塊實現返回從他們被稱爲其中的價值。

注 - 將字符串替換爲塊定義中的字符串,因爲您從API獲取字典而不是字符串。

Learn block syntax

+0

謝謝帕旺,它的工作原理。 – Lenin

+0

@Lenin請提出並接受答案。 – Pawan