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

Greg picture Greg · Apr 28, 2011 · Viewed 9.6k times · Source

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

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...

Answer

Jon Reid picture Jon Reid · May 2, 2011

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

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

Alternatively, you could use OCHamcrest to say:

assertThat(nsMutableArrayInstance, hasCountOf(5));