[英]NSArray and NSInteger Comparison
NSArray *array=[[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
NSNumber *j = @(1);
for(id i in array) {
if ([i isEqual:j])// always executes an else..I don't know why
{
NSLog(@"This object is matching %@",i);
}
else
{
NSLog(@"This Object is missing %@",i);
}
j = @([j intValue] + 1);
}
the code always executes the else. 该代码始终执行else。 I don't know why. 我不知道为什么 I want to check the array elements whether some missing in a list or not 我想检查数组元素是否在列表中丢失
您使用@"..."
将字符串放入数组中,但应使用整数:
NSArray *array=[[NSArray alloc] initWithObjects:@1,@2,@3,@4,@5,@6,@7,@8,@9,@10, nil];
You are comparing NSString
s from your array with the NSNumber
j
, so the comparison always fails 您正在将数组中的NSString
与NSNumber
j
进行比较,因此比较总是失败
One way would be to use the stringValue
of j
instead: 一种方法是改用j
的stringValue
:
NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
NSNumber *j = @(1);
for(NSString *i in array)
{
if ([i isEqualToString:j.stringValue])
{
NSLog(@"This object is matching %@",i);
}
else
{
NSLog(@"This Object is missing %@",i);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.