[英]Making an Integer Array in Objective-C
I want to have an internal int array for my class, but I can't seem to get XCode to let me. 我想为我的班级使用一个内部int数组,但是我似乎无法让XCode让我。 The array size needs to be set on initialization so I can't put the size directly into the interface. 数组大小需要在初始化时设置,因此我无法将大小直接放入接口中。
At the moment I've been trying: 目前,我一直在尝试:
@interface TestClass : NSObject {
int test[];
}
But it tells me that I'm not allowed. 但这告诉我,我不允许。 How to I refer to it in my interface, and then how do I allocate it when I create the implementation? 如何在我的界面中引用它,然后在创建实现时如何分配它?
Sorry for a somewhat standard sounding question, but I can't seem to find the answer I need from searching. 抱歉,听起来有些标准,但是我似乎无法通过搜索找到所需的答案。
edit: I want to use an array because it's apparently much faster than using an NSArray 编辑:我想使用数组,因为它显然比使用NSArray要快
You can use a number of methods to overcome this problem, but the easiest is to simply make the instance variable a pointer, like this: 您可以使用多种方法来克服此问题,但最简单的方法就是简单地使实例变量成为指针,如下所示:
@interface TestClass : NSObject {
int *test;
}
@property int *test;
@end
Synthesizing the property will give it getter and setter methods which you can use to set its contents: 合成属性将为其提供getter和setter方法,您可以使用它们来设置其内容:
@implementation TestClass
@synthesize test;
//contents of class
@end
You can then use it like this: 然后可以像这样使用它:
TestClass *pointerTest = [[TestClass alloc] init];
int *array = (int *)malloc(sizeof(int) * count);
//set values
[pointerTest setTest:array];
[pointerTest doSomething];
However, using objects like NSNumber
in an NSArray
is a better way to go, perhaps you could do something like this: 但是,在NSArray
使用类似NSNumber
对象是一种更好的方法,也许您可以执行以下操作:
@interface TestClass : NSObject {
NSArray *objectArray;
}
@property (nonatomic, strong) NSArray *objectArray;
@end
@implementation TestClass
@synthesize objectArray;
//contents of class
@end
You can then set its contents with a pointer to an NSArray
object: 然后可以使用指向NSArray
对象的指针设置其内容:
NSArray *items = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];
TestClass *arrayClass = [[TestClass alloc] init];
[arrayClass setItems:items];
[arrayClass doSomething];
When retaining objects upon setting them (like the previous example), always make sure you deallocate the object in the classes dealloc
method. 在设置对象时保留对象时(如前面的示例),请始终确保在类dealloc
方法中取消分配对象。
AC array is just a sufficiently sized raw memory buffer. AC阵列只是一个足够大的原始内存缓冲区。 Foundation has a nice wrapper around raw memory that frees you from all the manual memory management: NSMutableData
Foundation在原始内存周围有一个很好的包装器,可将您从所有手动内存管理中解放出来: NSMutableData
The following approach gives you automatic memory management plus proper encapsulation. 以下方法为您提供自动内存管理以及适当的封装。
@interface TestClass : NSObject
@property (nonatomic, readonly) int *testArray;
@property (nonatomic, readonly) NSUInteger testArraySize;
@end
@implementation TestClass
{
NSMutableData *_testData;
}
- (id)initWithSize:(NSUInteger)size
{
self = [self init];
if (self != nil) {
_testData = [NSMutableData dataWithLength:size];
}
}
- (int *)testArray
{
return [_testData mutableBytes];
}
- (NSUInteger)testArraySize
{
return [_testData length];
}
@end
As you see, the ivar does not have to be declared in the @interface. 如您所见,不必在@interface中声明ivar。
Try something like this: 尝试这样的事情:
@interface TestClass : NSObject
{
int *_test;
}
@property (assign) int *test;
@end
@implementation TestClass
- (instancetype)init
{
if (self = [super init])
{
_test = malloc(sizeof(int) * 20);
}
return self;
}
- (int *)test
{
return _test;
}
- (void)setTest:(int*)test
{
memcpy(&_test, &test, sizeof(_test));
}
- (void)dealloc
{
free(_test);
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.