2015-04-23 15 views
-3

我在didreceiveresponse方法中使用未聲明的標識符'data'。我導入了聲明瞭數組和數據的Model頭文件。我發現如果我在viewcontroller.h文件中聲明它們,錯誤就會消失。這個問題的原因是什麼?使用未聲明的標識符objective-c

#import "ViewController.h" 
    #import "DetailViewController.h" 
    //#import "Model.h" 
    #import "Model.h" 

    @interface ViewController() 

    @end 

    @implementation ViewController 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 

     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

     NSURL *url = [NSURL URLWithString:@"https://s3.amazonaws.com/jon-hancock-phunware/nflapi-static.json"]; 

     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

     [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

     // Do any additional setup after loading the view, typically from a nib. 
    } 

    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

     data = [[NSMutableData alloc]init]; 

    } 

這是模型頭文件。

#import <UIKit/UIKit.h> 

@interface Model : UIViewController 

    //NSArray *bars; 
    //NSMutableData *data; 



@property (nonatomic, retain) NSArray *bars; 
@property (nonatomic, retain) NSMutableData *data; 

@end 

修復了定義viewcontroller.h文件內部變量的錯誤。

進口

@interface ViewController : UIViewController { 

    IBOutlet UITableView *mainTableView; 
    NSArray *bars; 
    NSMutableData *data; 

} 
@end 

回答

1

這是Objective-C的不斯威夫特。您可以使用self.data或_data訪問屬性。除非你明確表達它,否則它不太清楚你的全局變量。另外,我會傾向於self.data直接訪問_data,因此setter會被調用。在這種情況下,這幾乎肯定不會成爲問題,但直接用下劃線設置ivars可能會使關鍵值觀察陷入困境,並可能產生難以調試的意想不到的後果。

+0

這只是更改錯誤,以使用在ViewController類型的對象上未找到的未聲明的標識符'_data'或屬性'data'。 –

+2

@MbusiHlatshwayo在你的Model類中使用它。你的ViewController甚至不聲明屬性。 – Schemetrical

+0

這裏沒有全局變量; ivar的名字是'_data',它可以被這個名字訪問,但是_只在宣告it_的類中。 –