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];
}
由另一个类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:没有被调用,因此字典属性没有被初始化。因此我将numberOfEntries设为0并且不执行循环。该怎么办?
答案 0 :(得分:1)
您需要一个完成处理程序,因为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);
}
完成处理程序返回字典以及潜在的序列化错误。
现在使用
调用方法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);
}
}
}];
完成处理程序中返回的参数只是一个示例。您也可以传递共享实例或任何您需要的实例。
您还应该实现connectionDidFailWithError
并在那里调用完成处理程序以返回nil
和错误。