使用AFNetworking 1.0为特定文件禁用ARC

时间:2013-11-15 14:28:12

标签: ios json xcode parsing

我在我的应用中使用了AFNetworking 1.0。我已经尝试过NetworkingIsFun这个例子,这正是我在我的应用中所需要的。我已将AFNetworking文件导入到我的项目中,并通过为编译源中的所有.m文件键入 -fno-objc-arc 来禁用ARC模式。

在示例中,我可以成功地将我的JSON文件解析为表视图,但在我的应用程序中没有任何反应。这与禁用的ARC模式文件有关吗?

这是我使用的代码:

#import "Pictures.h"
#import "DEMONavigationController.h"
#import "PicturesObject.h"
#import "AFNetworking.h"

@interface Pictures ()

@end

@implementation Pictures

@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView, movies = _movies;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Pictures";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:(DEMONavigationController *)self.navigationController
                                                                            action:@selector(showMenu)];

    self.tableView.separatorColor = [UIColor clearColor];

    // Setting Up Activity Indicator View
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
    self.tableView.separatorColor = [UIColor clearColor];

    // Initializing Data Source
    self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://my-json-file.com/picture.php?name=Name"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = JSON;
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];

}

// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.movies && self.movies.count) {
        return self.movies.count;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 321;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"PicturesObject";

    PicturesObject *cell = (PicturesObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PicturesObject" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
    cell.title.text = [movie objectForKey:@"title"];
    cell.published.text = [movie objectForKey:@"published"];

    NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:@"link"]];
    [cell.pic setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    return cell;
}

@end

让我再说一遍: 此代码适用于演示应用程序。但是当我为所有AFNetworking文件禁用ARC时,不在我的应用程序中。

为什么呢?有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Success Code here *you maybe have to parse JSON yourself I can't remember*
        self.movies = responseObject;
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // Failure Code here
    }];

您可以在AFNetworking 2.0中执行此操作。