Вопрос

I tried to use macros to make the change more flexible for adding new items. But somehow the frame won't be the size I would expected. If I change ANNOMENUV_NUMBER_OF_ITEMS directly to 5 then it would work like expected. Can anybody tell me why this happened?

#define ANNOMENUV_ITEMS @[@"Text",@"Font",@"Bold",@"Italic",@"Underline",@"Color"]
#define ANNOMENUV_NUMBER_OF_ITEMS [ANNOMENUV_ITEMS count]-1
#define ANNOMENUV_ITEM_WIDTH 45
#define ANNOMENUV_ITEM_CLOSE_WIDTH 45

- (id)initWithFrame:(CGRect)frame{

    //returns correctly the 5 items
    NSLog(@"Count %d",ANNOMENUV_NUMBER_OF_ITEMS);

    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y,ANNOMENUV_ITEM_WIDTH*ANNOMENUV_NUMBER_OF_ITEMS+ANNOMENUV_ITEM_CLOSE_WIDTH, ANNOMENUV_SIZE_HEIGHT)];

    if (self) {
        self.items = [NSMutableArray arrayWithArray: ANNOMENUV_ITEMS];
    }
    return self;
}
Это было полезно?

Решение

Try this,

#define ANNOMENUV_NUMBER_OF_ITEMS ([ANNOMENUV_ITEMS count]-1)

But, why?

These macro's are simply substituted in the respective places during compile time. So your actual init line will look like this after compiling (just ignore my above answer for now).

Consider ANNOMENUV_SIZE_HEIGHT = 35.

self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, 45*[@[@"Text",@"Font",@"Bold",@"Italic",@"Underline",@"Color"] count]-1+ 45, 35)];

Now if you recall operator precedence, you will understand why your code failed.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top