問題是GLKVector3
是C風格struct
,不是對象。所以它不知道如何應對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
那將copythe的someVector
內容到NSValue
,然後將它們重新複製出到storedVector
。
您可以使用valueWithPointer:
和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];
感謝您的回覆。 Objective-C的指針,以「浮動*」的隱式轉換是不允許用圓弧 – samb90 2013-02-22 21:21:17
在這是否發生什麼行:現在不知道如何糾正它,我得到這個錯誤? – Tommy 2013-02-22 22:14:30