我的代碼:二維數組的目標C從C#
float[2][] fSlvPipePtsC;
fSlvPipePtsC[0] = CGPointMake(0, 0);
fSlvPipePtsC[1] = CGPointMake(0, 1);
我從C#轉換的代碼。請幫我實現這個目標是C嗎?
我的代碼:二維數組的目標C從C#
float[2][] fSlvPipePtsC;
fSlvPipePtsC[0] = CGPointMake(0, 0);
fSlvPipePtsC[1] = CGPointMake(0, 1);
我從C#轉換的代碼。請幫我實現這個目標是C嗎?
我有。檢查下面的代碼。
NSMutableArray *fSlvPipePtsC = [[NSMutableArray alloc] initWithCapacity:2];
CGPoint new1 = CGPointMake(5.5,10.0);
CGPoint new2 = CGPointMake(0.0, 11.0);
[fSlvPipePtsC insertObject:[NSValue valueWithCGPoint:new1] atIndex:0];
[fSlvPipePtsC insertObject:[NSValue valueWithCGPoint:new2] atIndex:1];
i之後可以使用像下列:
CGPoint PointFp3 = CGPointMake([[fSlvPipePtsC objectAtIndex:0] CGPointValue].x, [[fSlvPipePtsC objectAtIndex:0] CGPointValue].y);
CGPoint PointFp4 = CGPointMake([[fSlvPipePtsC objectAtIndex:1] CGPointValue].x, [[fSlvPipePtsC objectAtIndex:1] CGPointValue].y);
目前尚不清楚你想要什麼。你想要一些點數?在Objective-C NSArray
中,s只能包含對象,而不能包含標量值。
如果你想創建點的C數組,你可以這樣做:
CGPoint[] pointArray;
int pointCount = 10;
pointArray = (CGPoint*) calloc(pointCount, sizeof(CGPoint));
pointArray[0] = CGPointMake(0, 0);
pointArray[1] = CGPointMake(0, 1);
//...
free((void*)pointArray);
如果你這樣做,那麼責任在於你無法超越你分配內存的界限,也可以釋放您分配完的內存。 Objective-C的ARC內存管理不包括您使用malloc()
創建的對象。
或者您可以在NSValue
包裹CGPoint對象,然後你可以存儲NSValue
S IN的NSArray
或NSMutableArray
NSMutableArray *pointArray = [NSMutableArray new];
CGPoint point = CGPointMake(2, 4);
NSValue *pointValue = [NSValue valueWithCGPoint:point];
[pointArray append: pointValue];
//...
CGPoint somePoint = [pointArray[0] CGPointValue];
(聲明:我在斯威夫特工作大多這些天,我的Objective-C在上面的代碼中可能會出現語法錯誤,它的目的是作爲指導,而不是代碼被複制/粘貼。)
請注意,Swift更像C#。你可以自由地創建一個CGPoint對象數組,並直接附加到它,等等。如果你來自C#,你可能會更快樂地學習Swift。此外,這是行業發展的方向,所以你最好學習Swift。
只是說明,在C#爲不是2-d陣列,而是鋸齒狀陣列(或陣列陣列)。一個2-D數組看起來像'float [,]'。這並不回答你的問題,但正確的術語可能會幫助你找到答案。 – pinkfloydx33
也作爲6k代表的人,你應該知道'CGPointMake'的實現可能與答案有關。同樣,你可能想要提供它。 – pinkfloydx33