在objective-c中将NSUInteger与int(例如5)进行比较的最快方法是什么?
背景 - 我注意到以下代码行给出了错误:
STAssertEquals([nsMutableArrayInstance count], 5, @"xxxx");
// gives Type Mismatch
所以我有效地问的是如何纠正这个以修复错误......
在objective-c中将NSUInteger与int(例如5)进行比较的最快方法是什么?
背景 - 我注意到以下代码行给出了错误:
STAssertEquals([nsMutableArrayInstance count], 5, @"xxxx");
// gives Type Mismatch
所以我有效地问的是如何纠正这个以修复错误......
STAssertEquals
要求您将相似类型与相似类型进行比较。因此,将“U”添加到数字以使其成为无符号文字:
STAssertEquals([nsMutableArrayInstance count], 5U, nil);
或者,您可以使用 OCHamcrest 说:
assertThat(nsMutableArrayInstance, hasCountOf(5));
NSUInteger i = 42;
int j = 5;
if (i > j) {
NSLog(@"the universe has not ended yet");
}
而不是使用STAssertEquals
,你可以使用STAssertTrue
:
STAssertTrue([nsMutableArrayInstance count] == 5, @"xxxx");
采用
STAssertEquals([nsMutableArrayInstance count], (NSUInteger)5, @"xxxx");
(NSUInteger)5
看起来不像干净,5U
但在编译 64 位时它也能正常工作。