問題は、それがオブジェクトではなくGLKVector3
C スタイルであることです。そのため、 orstruct
に応答する方法がわからないため、 では機能しません。retain
release
NSArray
できることはNSValue
、オブジェクト型である にそれぞれをラップすることであり、その中に任意の C 型を保持する方法を知っています。CとObjective-Cの境界にまたがっているので、特にきれいではありませんが、たとえば
GLKVector3 someVector;
[array addObject:[NSValue valueWithBytes:&someVector objCType:@encode(GLKVector3)]];
...
GLKVector3 storedVector;
NSValue *value = ... something fetched from array ...;
[value getValue:&storedVector];
// storedVector now has the value of someVector
someVector
これにより、 の内容がにNSValue
コピーされ、再び にコピーされstoredVector
ます。
コンテンツをコピーするのではなく、参照を配列に保持したい場合はvalueWithPointer:
andを使用できますが、手動のメモリ管理に注意する必要があるため、より良い解決策は次のように使用することです。pointerValue
someVector
NSData
// we'll need the vector to be on the heap, not the stack
GLKVector3 *someVector = (GLKVector3 *)malloc(sizeof(GLKVector3));
[array addObject:[NSData dataWithBytesNoCopy:someVector length:sizeof(GLKVector3) freeWhenDone:YES]];
// now the NSData object is responsible for freeing the vector whenever it ceases
// to exist; you needn't do any further manual management
...
GLKVector3 *storedVector = (GLKVector3 *)[value bytes];