gpt4 book ai didi

iphone - objective-c : a C int array in a class interface?

转载 作者:行者123 更新时间:2023-11-30 14:22:48 25 4
gpt4 key购买 nike

我正在尝试将 C int 数组定义为类接口(interface)中的实例变量,以便类中的任何方法都可以访问它。

写累了

@interface aVCofMine : UIViewController{
int[] myArray;
}

@interface aVCofMine : UIViewController{
int myArray[];
}

但没有效果。

实际上是否可以在类接口(interface)中定义一个 C 数组[而不是 NSArray]作为实例变量?

最佳答案

C 数组需要动态分配或固定大小。所以你可以这样做:

@interface AVCOfMine : NSViewController {
int myArray[5];
}
@end

或者你也可以这样做

@interface AVCOfMine : NSViewController {
int *myArray;
}
@end

@implementation AVCOfMine

- (void)viewDidLoad {
myArray = malloc(sizeof(int) * 5);
}

- (void)dealloc {
free(myArray);
[super dealloc]; // include this line only if not using ARC
}
@end

前者强制数组始终具有相同的大小。后者允许您在运行时选择大小,但显然更复杂一些。哪个更好取决于具体情况。

关于iphone - objective-c : a C int array in a class interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13479012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com