iphone - используя NSInvocation: постоянное значение - PullRequest
2 голосов
/ 09 марта 2010

Я имею дело со старым проектом iPhone OS 2.x и хочу сохранить совместимость при разработке 3.x.

Я использую NSInvocation, это код, подобный этому

NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
       [cell methodSignatureForSelector:
                                    @selector(initWithStyle:reuseIdentifier:)]];
[invoc setTarget:cell];
[invoc setSelector:@selector(initWithStyle:reuseIdentifier:)];
int arg2 = UITableViewCellStyleDefault;  //????
[invoc setArgument:&arg2 atIndex:2];
[invoc setArgument:&identificadorNormal atIndex:3];
[invoc invoke];

чтобы иметь код таким образом, как 3.0 и 2.0, каждый из которых использует свой синтаксис.

У меня возникла проблема в строке, помеченной вопросительными знаками.

Проблема в том, что я пытаюсь присвоить arg2 постоянную, которая не была определена в OS 2.0. Поскольку все, что связано с NSInvocation - это делать вещи косвенно, чтобы избежать ошибок компилятора, как я могу косвенно установить эту константу в переменную? Что-то вроде executeSelector "присваивать значение переменной" ...

это возможно? спасибо за любую помощь.

Ответы [ 2 ]

1 голос
/ 09 марта 2010
NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
       [cell methodSignatureForSelector:
                                    @selector(initWithStyle:reuseIdentifier:)]];
[invoc setTarget:cell];
[invoc setSelector:@selector(initWithStyle:reuseIdentifier:)];

int arg2;

#if (__IPHONE_3_0)
arg2 = UITableViewCellStyleDefault;
#else
//add 2.0 related constant here
#endif  

[invoc setArgument:&arg2 atIndex:2];
[invoc setArgument:&identificadorNormal atIndex:3];
[invoc invoke];


#if (__IPHONE_3_0)
arg2 = UITableViewCellStyleDefault;
#endif  
1 голос
/ 09 марта 2010

UITableViewCellStyleDefault определяется как 0, поэтому вы можете использовать 0 везде, где вы обычно используете UITableViewCellStyleDefault. Кроме того, нет необходимости использовать NSInvocation, это будет делать:

UITableViewCell *cell = [UITableViewCell alloc];
if ([cell respondsToSelector:@selector(initWithStyle:reuseIdentifier:)])
    cell = [(id)cell initWithStyle:0 reuseIdentifier:reuseIdentifier];
else
    cell = [cell initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier];

-[UITableViewCell initWithFrame:reuseIdentifier:] все равно будет работать на 3.x, просто устарела.

...