2012-12-05 54 views
-1

這裏我試圖從mywebpage獲取html,bt somethng是錯誤的..這​​裏是我的代碼..我已經在我的controller.h文件中聲明瞭html字符串。NSURLConnection sendAsynchronousRequest NSData不能訪問外側塊

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *responde,NSData *Data,NSError *error){ 

      html=[[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding]; 
      //NSLog(@"%@",html); //WORKING 

     }]; 

    NSLog(@"%@",html); //NOT WORKING 

回答

0

是的,它只能在完成塊內部訪問,因爲只有在接收到來自web服務的數據後,回調纔會獲得「html」數據。你不能在塊外訪問它。

+0

那麼我應該怎麼做才能訪問已存儲的數據我的HTML變量從webserivce來的? – BaSha

+0

在塊內部訪問它,如果需要,可以將數據發送到其他方法。 –

3

在塊外使用__block NSString *html

編輯:

__block NSString *html; //please notice __block is with two underscores. 

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *responde,NSData *Data,NSError *error){ 

     html=[[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding]; 
     //NSLog(@"%@",html); 

    }]; 

NSLog(@"%@",html); 
相關問題