开发者

Vectors in Cocoa Touch / Objective-C

Is it possible to use vectors in Coca Touch? If so, what library must be included?

开发者_JAVA百科

I was following a general C tutorial on them an the only thing needed was to #include <vector>

But XCode states that there is no such file or directory.

Any pointer to a library that provides such functionality is appreciated.

Regards ~dhp


Objective-C is a superset of C, and therefore anything C will work with them. However, std::vector is a C++ class, which means the compiler has to be aware that you're going to use C, Objective-C and C++ code inside your program. You can do so by changing the extension of your source files from .m to .mm.

However, if you're still at the stage of learning Objective-C or C++, try to not mix too much C++ with it. C++ uses "non-POD types" (POD being "plain old data"), which are inherently incompatible with functions that take variadic arguments; under the hood, all Objective-C calls work that way, so that can make it complex to work with C++ types through Objective-C calls if you're not too sure about how it all works. You could use Cocoa's NSMutableArray class too.


I figured it out... Follow the 3 steps below:

  1. Extend the file name with .mm
  2. #include "vector"
  3. Use this code:

std::vector <<int>int> myVector(20);
instead of just using vector <<int>int> myVector(20);

Now you have a brand new C++ vector working in Objective-C.


how about:

struct Vector { float r, x, y, z; };
typedef struct Vector Vector;

CG_INLINE Vector
VectorMake(CGFloat x, CGFloat y, CGFloat z)
{
    Vector vector; vector.x = x; vector.y = y; vector.z = z;return vector;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜