2016-08-16 53 views
0

我有一個NSMutableArrayCGPoint。當我嘗試將對象添加到我的CGPoint可變我看到這個錯誤:從不兼容類型'id'指派給'CGPoint'(又名'struct CGPoint')

Assigning to 'CGPoint' (aka 'struct CGPoint') from incompatible type 'id'

這裏是我的代碼:

targetView.center = [arrayWithCoord objectAtIndex:1]; 

我試過的Xcode提示方式:

targetView.center = *((__bridge CGPoint *)([arrayWithCoord objectAtIndex:1])); 

我也試試這個:

targetView.center = ((NSValue *)arrayWithCoord[0]).CGPointValue; 

在所有情況下,我都有targetView.center = {0 , 0}。我絕對相信這個數組不是空的。看我的NSLog s:

2016-08-17 00:41:40.760 newword[16497:819894] Element NSPoint: {154.5, 31}

2016-08-17 00:41:40.760 newword[16497:819894] Element NSPoint: {197.5, 31}

2016-08-17 00:41:40.760 newword[16497:819894] Element NSPoint: {240.5, 31}

2016-08-17 00:41:40.761 newword[16497:819894] target center result {0, 0}

感謝您的幫助!

回答

3

它說刪除*從CGPoint

你寫

CGPoint * 

CGPoint 

只有

,並用於

NSValue *val = [arrayWithCoord objectAtIndex:0]; 
CGPoint p = [val CGPointValue]; 

,然後分配p來targetView.center

或只有一步做點

CGPoint p = [arrayWithCoord[0] CGPointValue]; 
+0

我的目標結果:2016-08-17 09:36:03.682新詞[17057:841214] P是{154.5,31} 2016-08-17 09:36:03.682新詞[17057:841214] target中心結果{0,0} –

+0

我的目標中心未設置,請檢查target.center = CGPointMake(px,py); –

2

什麼是你正在試圖改變target

用於從數組中的NSValue解開CGPoint的代碼應該可以正常工作。您可以通過將其分配給一個臨時變量來進行測試,將其打印出來,然後將其分配爲center

是否將中心設置爲例如@{3,5}工作的任意點?

+0

我嘗試通過點擊移動我的按鈕。 targetView是一個UIImages。 [截圖](https://monosnap.com/file/4xR5yYVcRQK4YkAvD1YHruCctBpNUF.png) –

相關問題