2011-08-01 75 views
1

的比较我有一个条件语句,我需要一些帮助的语法:指针和整数问题

if (mySegment.selectedSegmentIndex == 0 && [[[dict objectForKey:[keys objectAtIndex:row]] objectAtIndex:0] == 8) { 
NSLog(@"test"); 
} 

我得到一个comparison between a pointer and integer problem警告。我需要一个基于选择的段值的条件,然后将其与从字典对象返回的值进行比较。

感谢您的任何帮助。

回答

6

转换dict值转换成整数,然后比较

试试这个:

if (mySegment.selectedSegmentIndex == 0 && [[[dict objectForKey:[keys objectAtIndex:row]] objectAtIndex:0]intValue] == 8) { 
NSLog(@"test"); 
} 
+0

完美运作!谢谢。我无法弄清楚在哪里把“intValue” – hanumanDev

+0

完美的工作..谢谢。 –

6

NSDictionary对象只能包含NSNumber实例,而不能包含整数等原始标量类型。这就是为什么你会收到警告。

试试这个行:

if (mySegment.selectedSegmentIndex == 0 && [[[[dict objectForKey:[keys objectAtIndex:row]] objectAtIndex:0] integerValue] == 8) { 
... 
} 

注意对对象从数组返回我添加了一个调用integerValue

+0

哦,太好了,我完全忘了方法调用,并跳进C语法。我在想如果我应该删除我的答案或让它成为答案。 +1虽然。 –

2

[[[dict objectForKey:[keys objectAtIndex:row]] objectAtIndex:0]返回一个对象,该对象必须在对整数进行比较之前将其类型转换为int。

((int)[[[dict objectForKey:[keys objectAtIndex:row]] objectAtIndex:0])== 8应该为你做的伎俩。