Dividing variables in Objective-C
so I need to divide 开发者_高级运维on variable by a number. How can I do this? I know about the C functions of DIV and MOD, but do not know how to use them in objective-C/cocoa-touch. Heres an example of my code.
// hide the previous view
scrollView.hidden = YES;
//add the new view
scrollViewTwo.hidden = NO;
NSUInteger across;
int i;
NSUInteger *arrayCount;
// I need to take arrayCount divided by three and get the remainder
When I try to use / or % I get the error "Invalid operands to binary expression ('NSUInteger and int) Thanks for any help
Firstly, should arrayCount
really be a pointer?
Anyway, if arrayCount
should be a pointer, you just need to dereference it...
NSInteger arrayCountValue = *arrayCount;
... and use the operators /
(for division) and %
(for getting the module):
NSInteger quotient = arrayCountValue / 3;
NSInteger rest = arrayCountValue % 3;
You can do it without the auxiliary variable too:
NSInteger quotient = *arrayCount / 3;
NSInteger rest = *arrayCount % 3;
And just remove the dereference operator *
if arrayCount
is not a pointer:
NSInteger quotient = arrayCount / 3;
NSInteger rest = arrayCount % 3;
精彩评论