For iPad / iPhone apps, how to have an array of points?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 12:46:08

问题


We can have an NSMutableArray object, and add objects to it.

But CGPoint is not an object... are there Point objects suitable to be added to an NSMutableArray object?

I see some code using NSStringFromCGPoint to create an NSString object so that it can be added to the array, and later use CGPointFromString to get the CGPoint back... but that just looks too much of a hack...


回答1:


You can store the points in the array using NSValue as a wrapper:

CGPoint a = CGPointMake(10.0, 10.0);
[array addObject:[NSValue valueWithCGPoint:a]];

CGPoint b = [(NSValue *)[array objectAtIndex:0] CGPointValue];



回答2:


You should use the NSValue class with the following methods:

+ valueWithCGPoint:
- CGPointValue



回答3:


You have to choose between...

A. c pointer array (look here)

or

B. NSValue or your NSStringFromCGPoint. Like this: [NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)]



来源:https://stackoverflow.com/questions/10262254/for-ipad-iphone-apps-how-to-have-an-array-of-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!