NSURLConnection 不下载

NSURLConnection not downloading

JSONDownload.m

中的函数
-(void)downloadEntries{
    NSString *urlString = @"https://itunes.apple.com/in/rss/topmovies/limit=50/genre=4431/json";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [self.webData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data{
    [self.webData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSError *error;
    self.dictionary = [NSJSONSerialization JSONObjectWithData:self.webData options:0 error:&error];
    NSLog(@"%@", [error localizedDescription]);
}

-(NSString *)returnName: (NSInteger)index{
    NSArray *entry = [self entryArray];
    NSDictionary *indexDictionary = [entry objectAtIndex:index];
    NSDictionary *nameDictionary = [indexDictionary objectForKey:@"im:name"];
    NSString *nameOfMovie = [nameDictionary objectForKey:@"label"];
    return nameOfMovie;
}

-(NSInteger)numberOfEntries{
    NSDictionary *feed = [self dictionary];
    NSArray *entry = [feed objectForKey:@"entry"];
    return [entry count];
}

被另一个人调用 class FeedEntry.m

- (void)recieveEntries{
    JSONDownload *download = [JSONDownload sharedInstance];
    [download downloadEntries]; // calling method

    NSInteger numberOfEntries = [download numberOfEntries]; //calling method

    for (int index = 0; index < numberOfEntries; index++) {

        self.name = [download returnName:index]; //calling method 
        NSLog(@"%@",self.name);
    }
}

但是在跟踪时我发现 connectionDidFinishLoading: 没有被调用,因此 dictionary 属性 在那个函数中没有被初始化。因此,我得到的 numberOfEntries 为 0,并且没有执行循环。应该怎么办?

您需要一个完成处理程序,因为 NSURLConnection 异步工作。

FeedEntry.h中定义一个块类型和一个completion属性

typedef void (^ConnectionCompletion)(NSDictionary *data, NSError *error);

@property (nonatomic, copy) ConnectionCompletion completion;

FeedEntry.m 中将 downloadEntries 更改为

- (void)downloadEntriesWithCompletion:(ConnectionCompletion)completion{
    self.completion = completion;
    NSString *urlString = @"https://itunes.apple.com/in/rss/topmovies/limit=50/genre=4431/json";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}

并将connectionDidFinishLoading更改为

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSError *error;
    self.dictionary = [NSJSONSerialization JSONObjectWithData:self.webData options:0 error:&error];
    self.completion(self.dictionary, error);
}

完成处理程序 return 字典以及潜在的序列化错误。

现在用

调用方法
JSONDownload *download = [JSONDownload sharedInstance];
[download downloadEntriesWithCompletion:^(NSDictionary *data, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else {
       NSInteger numberOfEntries = [download numberOfEntries]; //calling method
       for (int index = 0; index < numberOfEntries; index++) {
         self.name = [download returnName:index]; //calling method 
         NSLog(@"%@",self.artist);
       }
     }
 }];

完成处理程序中的 returned 参数只是一个示例。您也可以传递共享实例或任何您需要的东西。

您还应该实现 connectionDidFailWithError 并在那里调用完成处理程序以 return nil 和错误。