@implementation MonthView {
DayView *dayViews[6][7];
}
Xcode does not complain about this code, but AppCode gives a warning:
Xcode不会抱怨此代码,但AppCode会发出警告:
Pointer to non-const type 'DayView * * const * ' with no explicit lifetime
指向非const类型'DayView * * const *'的指针,没有明确的生命周期
My intention was to create a 6x7 block of DayView pointers that will be part of the memory layout of any MonthView instance.
我的目的是创建一个6x7的DayView指针块,它将成为任何MonthView实例的内存布局的一部分。
Is this code doing what I want, and how can I fix this warning?
这段代码是否符合我的要求,如何修复此警告?
1 个解决方案
#1
1
What you're attempting to do is valid, but if the comments above are correct and this is due to a bug in AppCode and the warning you receive throws a wrench into the works (such as when using -Werror) or it just bothers you to receive it, you can get around it by just allocating the array inside -init.
您尝试做的是有效的,但如果上面的评论是正确的,这是由于AppCode中的错误和您收到的警告会引发工作(例如使用-Werror时),或者只是困扰您接收它,你可以通过在-init中分配数组来解决它。
Fair warning: This code is off the top of my head and I don't guarantee it to work as written.
公平警告:此代码不在我的头顶,我不保证它按照书面工作。
@implementation MonthView {
DayView ***dayViews;
}
@interface MonthView
- (id)init {
if ((self = [super init])) {
int i;
// do stuff here
// Create the array
dayViews = malloc(sizeof(id) * 6);
dayViews[0] = malloc(sizeof(DayView *) * 6 * 7);
for (i = 1; i < 6; i++) {
dayViews[i] = dayViews[0] + (i * 7);
}
}
return self;
}
@end
This code should produce a two-dimensional array that you can access as normal, while minimizing the number of calls to malloc needed.
此代码应生成一个二维数组,您可以正常访问,同时最大限度地减少对malloc所需的调用次数。
#1
1
What you're attempting to do is valid, but if the comments above are correct and this is due to a bug in AppCode and the warning you receive throws a wrench into the works (such as when using -Werror) or it just bothers you to receive it, you can get around it by just allocating the array inside -init.
您尝试做的是有效的,但如果上面的评论是正确的,这是由于AppCode中的错误和您收到的警告会引发工作(例如使用-Werror时),或者只是困扰您接收它,你可以通过在-init中分配数组来解决它。
Fair warning: This code is off the top of my head and I don't guarantee it to work as written.
公平警告:此代码不在我的头顶,我不保证它按照书面工作。
@implementation MonthView {
DayView ***dayViews;
}
@interface MonthView
- (id)init {
if ((self = [super init])) {
int i;
// do stuff here
// Create the array
dayViews = malloc(sizeof(id) * 6);
dayViews[0] = malloc(sizeof(DayView *) * 6 * 7);
for (i = 1; i < 6; i++) {
dayViews[i] = dayViews[0] + (i * 7);
}
}
return self;
}
@end
This code should produce a two-dimensional array that you can access as normal, while minimizing the number of calls to malloc needed.
此代码应生成一个二维数组,您可以正常访问,同时最大限度地减少对malloc所需的调用次数。