当在类实例化时未定义维时,如何在Objective-C中将float数组声明为类变量?

时间:2022-05-17 10:29:00

In Java, it would look like this:

在Java中,它看起来像这样:

class Foo
{
  float[] array;
}

Foo instance = new Foo();
instance.array = new float[10];

3 个解决方案

#1


You can just use a pointer:

你可以使用一个指针:

float *array;
// Allocate 10 floats -- always remember to multiple by the object size
// when calling malloc
array = (float *)malloc(10 * sizeof(float));
...
// Deallocate array -- don't forget to do this when you're done with your object
free(array);

If you're using Objective-C++, you could instead do:

如果你使用的是Objective-C ++,你可以改为:

float *array;
array = new float[10];
...
delete [] array;

#2


Here's another way to do it. Create a NSMutableArray object and add NSNumber objects to it. It's up to you to decide whether or not this is sensible.

这是另一种方法。创建一个NSMutableArray对象并向其添加NSNumber对象。由你来决定这是否合情合理。

NSMutableArray *array;
array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:1.0f]];
[array release];

#3


Another way to do this in Objective-C is to use indexed instance variables:

在Objective-C中执行此操作的另一种方法是使用索引实例变量:

@interface ArrayOfFloats : NSObject {
@private
  NSUInteger count;
  float      numbers[0];
}
+ (id)arrayOfFloats:(float *)numbers count:(NSUInteger)count;
- (float)floatAtIndex:(NSUInteger)index;
- (void)setFloat:(float)value atIndex:(NSUInteger)index;
@end

@implementation ArrayOfFloats
+ (id)arrayOfFloats:(float *)numbers count:(NSUInteger)count {
    ArrayOfFloats *result = [NSAllocateObject([self class], count * sizeof(float), NULL) init];
    if (result) {
        result->count = count;
        memcpy(result->numbers, numbers, count * sizeof(float));
    }
    return result;
}
...
@end

For more see the documentation for NSAllocateObject(). A limitation of indexed instance variables is that you can't subclass a class that uses them.

有关更多信息,请参阅NSAllocateObject()的文档。索引实例变量的限制是您不能子类化使用它们的类。

#1


You can just use a pointer:

你可以使用一个指针:

float *array;
// Allocate 10 floats -- always remember to multiple by the object size
// when calling malloc
array = (float *)malloc(10 * sizeof(float));
...
// Deallocate array -- don't forget to do this when you're done with your object
free(array);

If you're using Objective-C++, you could instead do:

如果你使用的是Objective-C ++,你可以改为:

float *array;
array = new float[10];
...
delete [] array;

#2


Here's another way to do it. Create a NSMutableArray object and add NSNumber objects to it. It's up to you to decide whether or not this is sensible.

这是另一种方法。创建一个NSMutableArray对象并向其添加NSNumber对象。由你来决定这是否合情合理。

NSMutableArray *array;
array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:1.0f]];
[array release];

#3


Another way to do this in Objective-C is to use indexed instance variables:

在Objective-C中执行此操作的另一种方法是使用索引实例变量:

@interface ArrayOfFloats : NSObject {
@private
  NSUInteger count;
  float      numbers[0];
}
+ (id)arrayOfFloats:(float *)numbers count:(NSUInteger)count;
- (float)floatAtIndex:(NSUInteger)index;
- (void)setFloat:(float)value atIndex:(NSUInteger)index;
@end

@implementation ArrayOfFloats
+ (id)arrayOfFloats:(float *)numbers count:(NSUInteger)count {
    ArrayOfFloats *result = [NSAllocateObject([self class], count * sizeof(float), NULL) init];
    if (result) {
        result->count = count;
        memcpy(result->numbers, numbers, count * sizeof(float));
    }
    return result;
}
...
@end

For more see the documentation for NSAllocateObject(). A limitation of indexed instance variables is that you can't subclass a class that uses them.

有关更多信息,请参阅NSAllocateObject()的文档。索引实例变量的限制是您不能子类化使用它们的类。