你没有错过任何东西;对于 c 标量类型的数组,没有正式的 objc 接口。
简单的方法(如westsider所说)是使用std::vector,然后使用CF/NS-Data等机制实现序列化/反序列化。
如果需要,您可以将 std::vector 包装在 objc 接口中:
/* MONDoubleArray.h */
/* by using pimpl, i'm assuming you are not building everything as objc++ */
struct t_MONDoubleArray_data;
@interface MONDoubleArray : NSObject < NSCoding, NSCopying, NSMutableCopying >
{
t_MONDoubleArray_data* data;
}
- (double)doubleAtIndex;
- (void)setDoubleAtiIndex:(double)index;
- (NSUInteger)count;
/*...*/
@end
/* MONDoubleArray.mm */
struct t_MONDoubleArray_data {
std::vector<double> array;
};
@implementation MONDoubleBuffer
- (id)init
{
self = [super init];
if (0 != self) {
/* remember your c++ error handling (e.g., handle exceptions here) */
array = new t_MONDoubleArray_data;
if (0 == array) {
[self release];
return 0;
}
}
return self;
}
/*...more variants...*/
- (void)dealloc
{
delete array;
[super dealloc];
}
- (NSData *)dataRepresentationOfDoubleData { /*...*/ }
- (void)setDoubleDataFromDataRepresentation:(NSData *)data { /*...*/ }
/*...*/
@end
那么,你就可以毫不费力地完成 objc 序列化了。
还有一种方法可以将 CF/NS_MutableArray 用于标量,使用指针(或更窄)大小的条目:
@interface MONFloatBuffer : NSObject
{
NSMutableArray * floats;
}
@end
@implementation MONFloatBuffer
- (id)init
{
self = [super init];
if (0 != self) {
CFAllocatorRef allocator = 0; /* default */
CFIndex capacity = 0; /* resizable */
/* you could implement some of this, if you wanted */
const CFArrayCallBacks callBacks = { 0 /* version */ , 0 /* retain */ , 0 /* release */ , 0 /* copyDescription */ , 0 /* equal */ };
floats = (NSMutableArray*)CFArrayCreateMutable(allocator, capacity, &callBacks);
// now we can read/write pointer sized values to `floats`,
// and the values won't be passed to CFRetain/CFRelease.
}
return self;
}
@end
但是,如果没有自定义,它仍然无法正确反序列化自身。所以... NSPointerArray 可以轻松地完成 more ...但是您仍然固定为指针大小的值,因此您必须自己编写它。这并不难。缺点是您最终可能得到的变体数量。