Jem
Jem

Reputation: 6406

ObjC structs of floats in arrays: Compact way to avoid NSValue?

I'm to port some JS to native ObjC code. Since a struct won't fit inside arrays, it needs to be wrapped.

The JS code goes as follows:

var bezierVertices = [{0: 14},{10: 32},{24: 16}];

Plain and easy JS: Array of anonymous objects.

I'm bound to the following requirement: Have the code as compact as possible, meaning I've been refused when proposing an NSArray of NSValue using [NSValue valueWithCGPoint:ccp(x,y)]

Going down the malloc way doesn't fit this criterion either. They want something as compact as the JS stated above.

Before writing something as ugly as an NSString like @"0:14;10:32;24:16"; that's split and parsed in a loop, I thought SO could help bring something clean :) I'm allowed to use .mm so ObjC++ solutions could fit as well, but I'm not knowledgeable about C++ at all...

Thanks! J.

Upvotes: 0

Views: 137

Answers (2)

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

If you need to manage variable-length arrays of vertices, C++ provides std::vector<CGPoint>.

Upvotes: 1

JeremyP
JeremyP

Reputation: 86651

They want something as compact as the JS stated above

Who's "they"? Do "they" have any understanding that Objective-C is a compiled language and the "compactness" of the source code is largely irrelevant?

Anyway, rant over. You can make a C array of CGPoints like this:

CGPoint myArray[] = {{0.0, 14.0}, {10.0, 32.0}, {24.0, 16.0}}; 

This is a standard C array initialiser. You get the number of elements like this:

int nElements = sizeof myArray / sizeof(CGPoint);

Upvotes: 4

Related Questions