2
@implementation MonthView {
    DayView *dayViews[6][7];
}

Xcode does not complain about this code, but AppCode gives a warning:

Pointer to non-const type 'DayView * * const * ' with no explicit lifetime

My intention was to create a 6x7 block of DayView pointers that will be part of the memory layout of any MonthView instance.

Is this code doing what I want, and how can I fix this warning?

giampaolo
  • 6,906
  • 5
  • 45
  • 73
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133

1 Answers1

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.

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.

Rabbit
  • 566
  • 5
  • 13