开发者

Datatypes for Objective C

What Objective-C datatype would you recommend to store the following:

Position - The position for an object on th开发者_高级运维e screen

Direction - The direction of the object

Velocity - Well, the velocity of the object

Bounds - The allowed bounds for where the object is allowed to be.

Thanks!

Björn


Objective-C is a superset of C. For these sort of things you would normally use C structs, though you might alternatively make them fields of an object.

If your question is actually about predefined types in Cocoa, then there are options depending on the particular version (ie, platform -- Mac OS, iOS or I guess conceivably GNUStep) and particular purpose.

For basic 2D stuff, the simplest choice would probably be CGPoint for position, direction and velocity (assuming velocity is a vector quantity distinct from direction; otherwise just use a double) and CGRect for bounds.

(Arguably, it is a marginal semantic abuse to use CGPoint for non-position vectors, but I see no reason to be that picky.)


There are native or Library (Foundation classes) types/structs for almost any of this stuff, unless I am missing something, NSPoint and NSRect can cover almost anything you want ( areas, vectors, points ) . They may not be "Objective-C" datatypes but are available in the Library.


CoreLocation uses the CLLocation for all but the bounding and facing. However, I would not suggest using it unless you're using lat/long for your coordinate system and want to integrate with the CoreLocation GPS functionality.

There are other design considerations. Do you want the location of the object to be relative to its bounding box or in global coordinates? Do you need to store facing direction separate from the direction of velocity? Do you need high-resolution (which means floats) or do you need faster computation (which leans you towards integers)? Do you need to save it in a CoreData database? (NSManagedObject-derived object instead of structs.)

I was going to say use the structures of the third-party library you're going to use (UIKit, Cocos2d, OpenGL, or CoreLocation), but then I thought, "why limit yourself when you can add methods like -(CLLocationCoordinate2D)coreLocationCoordinate to your object."

I'd suggest creating your own struct or object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜