因此,我使用AFNetworking和pokeapi来获取pokemon的列表。我可以让表视图加载,标题是口袋妖怪的名称。但我希望副标题是那种。所以我把基础设为#define pokeApiBaseURL [NSURL URLWithString: @"http://pokeapi.co/"]
以下是我的请求。我创建了一个数组,它为我提供了我需要解析的url的下半部分,并得到了类型和所有口袋妖怪的其他信息。但我似乎想不出如何使baseURL和resource_uri创建一个新的url供我解析。
NSString *string = [NSString stringWithFormat:@"%@api/v1/pokedex/1/", pokeApiBaseURL];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.pokemonArray = [responseObject objectForKey:@"pokemon"];
self.pokemonDetailArray = [[responseObject objectForKey:@"pokemon"]valueForKey:@"resource_uri"];
// NSLog(@"\n%@",detailString);
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed: %@, %@", error, error.userInfo);
}];
[operation start];这是我把它加到牢房里的地方
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdenifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdenifier forIndexPath:indexPath];
NSDictionary *pokemonDictionary = [self.pokemonArray objectAtIndex:indexPath.row];
NSDictionary *pokemonDetailDictionary = [self.pokemonDetailArray objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.textLabel sizeToFit];
cell.textLabel.text = [pokemonDictionary objectForKey:@"name"];
cell.detailTextLabel.text = @"PLACEHOLDER"; //WANT THE TYPE TO GO HERE!
return cell;}
发布于 2015-11-30 09:50:35
编辑
今天早上,我有一些空闲时间,因为一个客户没回我的电话,于是就提了这个问题:https://github.com/AlexTrott/PokeAPI:这并不完美,它只是向您展示如何在第一次加载时本地存储数据,如果您有任何问题要告诉我的话。
以下是如何获取您想要的https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-3.0-Migration-Guide的URL和信息):
-(void)loadData {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://pokeapi.co/api/v1/pokedex/1/" parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
pokemonArray = [responseObject objectForKey:@"pokemon"];
pokemonDetailArray = [[responseObject objectForKey:@"pokemon"]valueForKey:@"resource_uri"];
[self loop];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)loop {
for (int i = 0; i < [pokemonArray count]; i++) {
[self loadMore:i];
}
}
-(void)loadMore:(int)pokemonID {
NSString *url = [NSString stringWithFormat:@"%@%@", pokeApiBaseURL, [pokemonDetailArray objectAtIndex:pokemonID]];
NSLog(@"URL = %@", url);
NSLog(@"Loop Number = %d", pokemonID);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"response = %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}我认为您需要重新考虑如何处理这个问题,我刚刚检查了poke api,为了做您想做的事情,您必须拨打779个电话,其中一个是pokedex调用,另一个是778口袋妖怪。
您可以通过删除pokedex请求来降低这个值,然后根据数据形成一个数组,但是然后执行778请求,在我看来,这是疯狂的,我认为您需要想出如何显示数据,如果第一次加载数据或者编译自己的数据并使其本地化,这是疯狂的,因为778请求是疯狂的。
如果你真的要这么做的话,一定要确保它只在第一次加载应用程序或其他东西的时候,然后存储在CD或领域之类的地方。
对于一个非常快速和肮脏的示例,要将您的数据放入一个排序的NSMutableArray中--看看这个,我不会这样做,我会对它进行批量处理,或者在加载时在本地存储,但是它会让您更好地了解您想要做的事情。
#import "ViewController.h"
#import <AFNetworking/AFNetworking.h>
@interface ViewController () {
NSString *pokeApiBaseURL;
NSMutableArray *pokedex;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTable) name:@"orginalDone" object:nil];
pokeApiBaseURL = @"http://pokeapi.co/";
pokedex = [[NSMutableArray alloc] init];
for (int i = 0; i < 151; i++) {
[self loadData:i];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)loadData:(int)pokemonID {
NSString *url = [NSString stringWithFormat:@"http://pokeapi.co/api/v1/pokemon/%d/", pokemonID];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
[self addPokemon:responseObject];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)addPokemon:(id)pokemonObject {
NSMutableDictionary *pokemonData = [[NSMutableDictionary alloc] init];
[pokemonData setObject:pokemonObject[@"pkdx_id"] forKey:@"pkmnID"];
[pokemonData setObject:pokemonObject[@"name"] forKey:@"pokemonName"];
NSMutableArray *types = [[NSMutableArray alloc] init];
for (int i = 0; i < [pokemonObject[@"types"] count]; i++) {
[types addObject:pokemonObject[@"types"][i][@"name"]];
}
[pokemonData setObject:types forKey:@"types"];
[pokedex addObject:pokemonData];
NSLog(@"count = %lu", (unsigned long)[pokedex count]);
if ([pokedex count] == 150) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"orginalDone" object:nil];
}
}
-(void)updateTable {
NSLog(@"Unsorted = %@", pokedex);
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"pkmnID" ascending:YES];
[pokedex sortUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"Sorted = %@", pokedex);
}
@end发布于 2015-11-30 07:13:12
我认为您可能在寻找将资源与基本资源组合在一起的方法:
+ (instancetype)URLWithString:(NSString *)URLString
relativeToURL:(NSURL *)baseURLhttps://stackoverflow.com/questions/33990855
复制相似问题