我被布尔显示/隐藏图片困住了很长时间。图像放在我的手机中。只需要布尔值。谁能告诉我我做错了什么?
PFQuery *query = [PFQuery queryWithClassName:@"Parseclass"];
[query whereKey:@"imagebool" equalTo:[NSNumber numberWithBool:[NSNumber numberWithBool:YES]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
cell.discounts.hidden =YES;
}else{
cell.discounts.hidden =NO;
}
}];
编辑:
PFQuery *query = [PFQuery queryWithClassName:@"parsecell"];
[query whereKey:@"imagebool" equalTo:[NSNumber numberWithBool:YES]];
[query findObjectsInBackgroundWithBlock:^(NSArray *object, NSError *error)
{
if (!error)
{
NSLog(@"Successfully retrieved: %@", object);
NSDictionary *dict = [object objectAtIndex:0];
BOOL boolean;
boolean = [[dict objectForKey:@"imagebool"] boolValue];
if(boolean==YES)
{
NSLog(@"BOOL1: %hhd", boolean);
cell.discounts.hidden = YES;
} else {
cell.discounts.hidden = NO;
NSLog(@"BOOL2: %hhd", boolean);
}
}
else
{
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
答案 0 :(得分:1)
更改以下内容: -
[NSNumber numberWithBool:[NSNumber numberWithBool:YES]] //No need for this double encoding.
简单地使用[NSNumber numberWithBool:YES]。
另外
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error && objects) //Checking for object's just to know if your getting any result for condition that you provided in query.
{
//Then hide your image.
}else{
//Or show your image.
}
}];
以防万一你的存储图像在解析并检查图像是否为零或存储。然后只是简单地调用存储图像的列而没有布尔检查条件。通过这种方式,您可以获得对象,并知道应用中的哪一行/哪一行是显示/隐藏图像。同样在该对象中,您将获得图像并在您的应用程序中提供它,而无需存储在手机的本地存储器中。其他任何事都让我知道。
更新: -
因为你将获得对象(数组)。
[query findObjectsInBackgroundWithBlock:^(NSArray *object, NSError *error)
{
if (!error)
{
NSLog(@"Successfully retrieved: %@", object);
NSDictionary *dict = [object objectAtIndex:0];
//Now with dict you could simply check for key(boolean) and then depending on YES/NO ,you could hide/Show the image.
}
else
{
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
希望这对你有所帮助。
答案 1 :(得分:0)
我在您的代码中看到两个问题:
YES
的双重编码,即[NSNumber numberWithBool:[NSNumber numberWithBool:YES]]
,可能不会破坏,但要修复它objects
。如果没有与查询匹配,则数组将返回空,添加对objects.count > 0