There is no direct way to take your own memory block and “convert” it cheaply into NSArray. In the end, the structure will then have to own this memory, and it does not know where you got it from (malloc, stack, etc.). If there was a convenience method for initWithArrayWrapElementsInObjects, he himself would have to do what you assume internally: iterate over its provided memory and add elements to itself (perhaps the infrastructure can do this as fast as memcpy, but who knows).
(, , ) NSArray, , (.. ), , NSArray. , NSArray count: objectAtIndex: , . , init/dealloc .. . http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
" ".
, . NSArray, , , Obj-C ( id), . C- , , . NSArray .
: NSMutableArray NSArray. , NSMutableArray NSArray, . NSMutableArray , NSArray, - .
UPDATE. , . , . NSArrays . ( ), initWithCapacity:. / , .
( ) ( MYArray MYMutableArray), NSArray-. NSArrays. .
# 2. , , , , . ( , NSArray float C-style). NSArray, float , . (?), :
@interface FloatProxyArray : NSArray
{
float * values;
NSUInteger count;
}
- (id)initWithCArray:(float *)arrayOfFloats count:(int)numberOfValues;
@end
.
@implementation FloatProxyArray
- (id)initWithCArray:(float *)arrayOfFloats count:(int)numberOfValues
{
if ((self = [super init])) {
values = (float *)malloc(numberOfValues * sizeof(float));
if (!values) {
[self release]; return nil;
}
memcpy(values, arrayOfFloats, numberOfValues * sizeof(float));
count = numberOfValues;
}
return self;
}
- (void)dealloc
{
free(values);
[super dealloc]
}
- (NSUInteger)count
{
return count;
}
- (id)objectAtIndex:(NSUInteger)index
{
if (index >= count) {
[NSException raise:NSRangeException format:@""];
return nil;
}
float val = values[index];
return [NSNumber numberWithFloat:val];
}
@end
(N.B. /.)