0

操作运行时,无法在由加速器创建的 JSON 模型中输入数据。你能告诉我我做错了什么吗?

{
    [super viewDidLoad];

NSLog(@"you are in a tableViewController");
self.title = @"NavigationOrdini";


NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stampa6x3.com/json.php?azione=ordini"]];
AFJSONRequestOperation* operation;

operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:req
                                                            success:^(NSURLRequest *request, NSURLResponse *response, id JSON)
{

    [[ordiniModel alloc] initWithDictionary:JSON];



}
             failure:^(NSURLRequest *request, NSURLResponse *response, NSError
                       *error, id JSON) {
                 [self setTitle:@"Dictionary"];
                 NSLog(@"failed! %d",[error code]);
             }];
[operation start];


ordiniModel*test;

NSLog(@"il valore è %@",test.ordini.description);

}
4

1 回答 1

0

AFJSONRequestOperation 是异步的,这意味着代码在应用程序的其余部分运行时继续执行。完成块在代码实际完成时运行。

所以试试:

NSLog(@"you are in a tableViewController");
self.title = @"NavigationOrdini";
ordiniModel *test;  // <-- create variable here

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stampa6x3.com/json.php?azione=ordini"]];
AFJSONRequestOperation* operation;

operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:req
                                                            success:^(NSURLRequest *request, NSURLResponse *response, id JSON)
{

    test = [[ordiniModel alloc] initWithDictionary:JSON]; // <-- Assign here
    NSLog(@"il valore è %@",test.ordini.description);


}
             failure:^(NSURLRequest *request, NSURLResponse *response, NSError
                       *error, id JSON) {
                 [self setTitle:@"Dictionary"];
                 NSLog(@"failed! %d",[error code]);
             }];
[operation start];
于 2013-03-26T20:34:55.693 回答