我需要检查我的值是否包含“ false”或字符串。
JSON:
{"success":true,"name":[{"image":false},{"image":"https:\/\/www.url.com\/image.png"}]}
我的代码:
NSData *contentData = [[NSData alloc] initWithContentsOfURL:url];
NSDictionary *content = [NSJSONSerialization JSONObjectWithData:contentData options:NSJSONReadingMutableContainers error:&error];
NSLog为我显示了第一个图像值:
NSLog(@"%@", content);
图片= 0;
我有一个UICollectionView,我想从URL设置图像。如果值“图像”为假,我想放置其他图像,但是我不知道如何检查它是否为假。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([[[content objectForKey:@"name"] objectAtIndex:indexPath.row] objectForKey:@"image"] == nil)
我也尝试过“ == false”“ == 0”,但是没有任何效果。
有人有主意吗?
当false
使用JSON时,它会像NSNumber
其中的Boolean一样反序列化false
。您可以进行如下比较:
// This is actually a constant. You can prepare it once in the static context,
// and use everywhere else after that:
NSNumber *booleanFalse = [NSNumber numberWithBool:NO];
// This is the value of the "image" key from your JSON data
id imageObj = [[[content objectForKey:@"name"] objectAtIndex:indexPath.row] objectForKey:@"image"];
// Use isEqual: method for comparison, instead of the equality check operator ==
if ([booleanFalse isEqual:imageObj]) {
... // Do the replacement
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句