【发布时间】:2015-01-06 07:20:54
【问题描述】:
我有 2 个网络服务,其中一个检索所有图像,另一个检索来自网络服务的过滤图像。
当应用程序加载时,它会调用检索所有图像的 Web 服务。当用户应用过滤器时,它会检索过滤后的图像。但我面临的问题是:
问题陈述:
当用户选择至少一个过滤器时,它工作正常。但是当用户取消选择(意味着没有选择任何过滤器)时,它会失败。我的 Web 服务的编码方式是,当没有传递任何参数时,它应该返回所有图像,但它没有。我希望它再次加载所有图像网络服务。
附代码说明:
[operation GET:@"stock_search" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
// operation is AFHTTPRequestOperationManager
NSMutableArray *temGalArray = [responseObject objectForKey:@"data"];
[imageArray removeAllObjects];
for (NSDictionary *myDict in temGalArray)
{
id object = [myDict objectForKey:@"square_image"];
if ([myDict objectForKey:@"square_image"]!=[NSNull null])
{
[imageArray addObject:myDict]; //this works fine
}
else if([object isEqual:[NSNull null]])
{
[self getGalleryFromWeb]; //***PROBLEM IS HERE***
//1: This condition is never true
//2: Self.getGalleryFromWeb is the webserivce that get
// all the images from web. There is no issue in that webservice
}
}
[galleryView reloadData];
}
//It always loads failure code below
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Applying Filters"
message:@"Check Your Internet Connection"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}];
}
那么我应该在“else if”下写什么,这样如果没有选择过滤器值,它会再次加载所有图像,就像加载应用程序时一样。我希望我已经解决了我的问题,因为这是我的第一个问题,所以如果有什么我想念的,我已经准备好提供。
【问题讨论】:
-
我知道 filter 是必需的参数,不是可选的...它返回什么?
-
@NilsHolgerson 我刚刚从 Web 服务人员那里确认了这一点。他说,如果没有参数,则返回所有图像。那么你能告诉我,我应该在
else if中写什么来检查是否没有参数吗?我试过检查[parameters isEqual:[NSNull null]]和parameters==nil甚至这个!parameters但它没有用。我也应该在else if或[imageArray addObject:myDict];下打电话给[self getGalleryFromWeb]; -
试着看看你的 [myDict objectForKey:@"square_image"] 是什么类。 NSLog 类。我认为你的 for 循环是错误的假设对象 isEqual,只需检查你的类,如 [object isKindOfClass:[NSNull class]]。我看到你做了一个 for 循环,所以做单独的逻辑步骤。首先添加你的对象。然后如果imageArray.count ==0,调用[self getGalleryFromWeb];
标签: ios objective-c json web-services