2011-09-24 55 views
0

所以我需要用變量除以數字。 我該怎麼做? 我知道DIV和MOD的C函數,但不知道如何在Objective-C/cocoa-touch中使用它們。這是我的代碼的一個例子。Objective-C中劃分變量

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

當我嘗試使用/或%我得到的錯誤 「無效操作數爲二進制表達式('NSUInteger和INT) 感謝您的幫助

+0

Xcod e是一種IDE而不是語言。 – Nick

回答

5

首先,arrayCount真的應該是一個指針?

無論如何,如果arrayCount是一個指針,你只需要取消對它的引用...

NSInteger arrayCountValue = *arrayCount; 

...並使用運營商/(除法)和%(用於獲取模塊):

NSInteger quotient = arrayCountValue/3; 
NSInteger rest = arrayCountValue % 3; 

你可以不用輔助變量太多:

NSInteger quotient = *arrayCount/3; 
NSInteger rest = *arrayCount % 3; 

而就如果arrayCount不是指針,請移除解除引用運算符*

NSInteger quotient = arrayCount/3; 
NSInteger rest = arrayCount % 3; 
+0

謝謝,這就是我所需要的 – Thermo