开发者

iphone - using NSInvocation: constant value

开发者 https://www.devze.com 2022-12-22 13:12 出处:网络
I am dealing with an old iPhone OS 2.x project and I want to keep compatibility, while designing for 3.x.

I am dealing with an old iPhone OS 2.x project and I want to keep compatibility, while designing for 3.x.

I am using NSInvocation, is a code like this

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];
开发者_如何学编程

to have a code in a way that both 3.0 and 2.0 like, each one using its proper syntax.

I am having a problem on the line I marked with question marks.

The problem there is that I am trying to assign to arg2, a constant that has not been defined in OS 2.0. As everything with NSInvocation is to do stuff indirectly to avoid compiler errors, how do I set this constant to a variable in an indirect way? Some sort of performSelector "assign value to variable"...

is that possible? thanks for any help.


UITableViewCellStyleDefault is defined as 0 so you can use 0 wherever you would normally use UITableViewCellStyleDefault. Also, there is no need to use an NSInvocation, this will do:

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:] will still work on 3.x, it's just deprecated.


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  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号