1

GLKVector3オブジェクトをNSMutableArrayに追加しようとしています。NSMutableArraysは特定のオブジェクトのみを受け入れることを理解しているので、私にとってもGLKVector3を配列に追加するのが最善の方法です。

コードのサンプルは次のとおりです。

        for(id basenormal in [jsnmvtx objectForKey:@"baseNormals"]){
            [basenormalsVectorArrays addObject:GLKVector3MakeWithArray(basenormal)];
        }

ありがとう

4

1 に答える 1

3

問題は、それがオブジェクトではなくGLKVector3C スタイルであることです。そのため、 orstructに応答する方法がわからないため、 では機能しません。retainreleaseNSArray

できることは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を使用できますが、手動のメモリ管理に注意する必要があるため、より良い解決策は次のように使用することです。pointerValuesomeVectorNSData

// 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];
于 2013-02-22T20:36:53.753 に答える