的功能在JSONDownload.mNSURLConnection的不下載
-(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,並且不執行循環。應該做什麼?
您是否實現了其他(必需的)委託方法'didReceiveResponse'和'didReceiveData'?無論如何,在'downloadEntries'之後調用'numberOfEntries'將總是返回0,因爲連接的異步行爲。 – vadian
@vadian yes我有 – Rishab
您需要一個完成處理程序或在'connectionDidFinishLoading'方法中調用的委託方法來通知'FeedEntry'數據可用。 – vadian