-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
這只是更改錯誤,以使用在ViewController類型的對象上未找到的未聲明的標識符'_data'或屬性'data'。 –
@MbusiHlatshwayo在你的Model類中使用它。你的ViewController甚至不聲明屬性。 – Schemetrical
這裏沒有全局變量; ivar的名字是'_data',它可以被這個名字訪問,但是_只在宣告it_的類中。 –