2011-07-31 84 views
1

這是我的第一篇文章,所以要友好:)比較CGPoint和NSNumber

我環顧四周尋找解決方案,但似乎無法找到一個。問題可能以谷歌將返回有用結果的方式提問。因此,我有一個NSMutableArray(稱爲boardColCoords),我有CGPoint(稱爲touchLocation)。我想比較兩者,以便我可以將UIImageView位置捕捉到boardColCoords數組中的適當座標。

下面是相關代碼:

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint touchLocation = [touch locationInView:self.view]; 

NSNumber *boardSquareX = [boardColCoords objectAtIndex:i]; 

if (touchLocation.x - boardSquareX <= 12) 
{ 
} 

我知道,12最終將不得不改變,但我只是想獲得減法第一個工作日。特定的錯誤我得到的是:

無效操作數的二進制表示(「CGFloat的」(又名「浮動」)和「的NSNumber *)

回答

8

的NSNumber是一個包裝,你要‘提取’它與價值像floatValue比較像:

if (touchLocation.x - [boardSquareX floatValue] <= 12) 
+0

謝謝,只要我有足夠的代表upvote,我會upvote這一點。 –

1

您需要從boardSquareX提取原始數

if (touchLocation.x - [boardSquareX floatValue] <= 12) 
{ 
} 
2

爲了得到一個浮點值fr。 om NSNumber,你會做

[boardSquareX floatValue]; 
+1

所有很好的答案。如果他們全都樂於助人,那麼爲信譽得分最低的人+1獎勵 – Ben

+0

@Ben爲什麼不把他們全部加滿?他們都花時間回答這個問題,並且正確回答... –

+0

只要我有足夠的代表,我就會立刻對所有這些問題滿意。感謝大家! –