xcode 在objective-c中将NSUInteger与int(例如5)进行比较的最快方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5814007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:55:57  来源:igfitidea点击:

What's the quickest way to compare a NSUInteger with an int (e.g. 5) in objective-c?

objective-cxcodexcode4ocunit

提问by Greg

What's the quickest way to compare a NSUInteger with an int (e.g. 5) in objective-c?

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

Background - I'm noting that the following line of code gives an error:

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

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

So what I'm asking effectively is how to correct this to fix the error...

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

采纳答案by Dave DeLong

NSUInteger i = 42;
int j = 5;

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

Instead of using STAssertEquals, you could use STAssertTrue:

而不是使用的STAssertEquals,你可以使用STAssertTrue

STAssertTrue([nsMutableArrayInstance count] == 5, @"xxxx");

回答by Jon Reid

STAssertEqualsrequires that you compare like types to like types. So add "U" to the number to make it an unsigned literal:

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

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

Alternatively, you could use OCHamcrest to say:

或者,您可以使用 OCHamcrest 说:

assertThat(nsMutableArrayInstance, hasCountOf(5));

回答by murat

Use

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

(NSUInteger)5does not look as clean as 5Ubut it will also work correctly when compiling for 64-bit.

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