在Objective-c中比较NSUInteger和int(例如5)的最快方法是什么?

时间:2011-04-28 05:26:16

标签: objective-c xcode xcode4 ocunit

在objective-c中比较NSUInteger和int(例如5)的最快方法是什么?

背景 - 我注意到以下代码行会出错:

STAssertEquals([nsMutableArrayInstance count], 5, @"xxxx");
// gives Type Mismatch

所以我有效地问的是如何纠正这个以修复错误......

3 个答案:

答案 0 :(得分:50)

STAssertEquals要求您将类似类型与类似类型进行比较。因此,在数字上添加“U”,使其成为无符号文字:

STAssertEquals([nsMutableArrayInstance count], 5U, nil);

或者,您可以使用OCHamcrest说:

assertThat(nsMutableArrayInstance, hasCountOf(5));

答案 1 :(得分:7)

NSUInteger i = 42;
int j = 5;

if (i > j) {
  NSLog(@"the universe has not ended yet");
}

您可以使用STAssertEquals

,而不是使用STAssertTrue
STAssertTrue([nsMutableArrayInstance count] == 5, @"xxxx");

答案 2 :(得分:3)

使用

STAssertEquals([nsMutableArrayInstance count], (NSUInteger)5, @"xxxx");

(NSUInteger)5看起来不像5U一样干净,但在编译64位时它也能正常工作。