【问题标题】:iphone - using NSInvocation: constant valueiphone - 使用 NSInvocation:常量值
【发布时间】:2010-03-09 01:20:13
【问题描述】:

我正在处理一个旧的 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 的一切都是间接地做一些事情以避免编译器错误,我如何以间接的方式将此常量设置为变量?某种 performSelector “为变量赋值”...

这可能吗?感谢您的帮助。

【问题讨论】:

  • 如果您使用较新的 SDK 编译,它将包含该常量(无论如何它只存在于编译时),并且当它被编译时,它仍然会 在旧操作系统上运行

标签: iphone cocoa iphone-sdk-3.0 ipad


【解决方案1】:

UITableViewCellStyleDefault 定义为0,因此您可以在通常使用UITableViewCellStyleDefault 的任何地方使用0。另外,不需要使用 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 上使用,只是已弃用。

【讨论】:

  • 谢谢。我没有尝试硬编码,但由于默认值为 0,看来他们将来不会更改它,因为 0 似乎是默认设置的好数字......但是,谁知道......
  • 他们不能在以后更改它,因为这会使所有旧代码中断。
【解决方案2】:
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  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多