在Objective-C中划分变量

The*_*rmo 0 cocoa-touch objective-c

所以我需要用变量除以一个数字.我怎样才能做到这一点?我知道DIV和MOD的C函数,但不知道如何在Objective-C/cocoa-touch中使用它们.这是我的代码的一个例子.

// 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
Run Code Online (Sandbox Code Playgroud)

当我尝试使用/或%时,我得到错误"二进制表达式的无效操作数('NSUInteger和int)感谢您的帮助

bra*_*zzi 5

首先,应该arrayCount真的是一个指针?

无论如何,如果arrayCount 应该是一个指针,你只需要取消引用它...

NSInteger arrayCountValue = *arrayCount;
Run Code Online (Sandbox Code Playgroud)

...并使用运算符/(用于除法)和%(用于获取模块):

NSInteger quotient = arrayCountValue / 3;
NSInteger rest = arrayCountValue % 3;
Run Code Online (Sandbox Code Playgroud)

你也可以在没有辅助变量的情况下完成它:

NSInteger quotient = *arrayCount / 3;
NSInteger rest = *arrayCount % 3;
Run Code Online (Sandbox Code Playgroud)

*如果arrayCount不是指针,只需删除解引用运算符:

NSInteger quotient = arrayCount / 3;
NSInteger rest = arrayCount % 3;
Run Code Online (Sandbox Code Playgroud)