iphone - 为什么执行选择器:withObject: methods can only take id?

标签 iphone objective-c c ios pointers

在 Objective-C 中,所有执行选择器的方法都类似于:

 - performSelector:withObject:
 - makeObjectsPerformSelector:withObject:
 - performSelectorInBackground:withObject:

并且参数中只能采用 id(指针)对象?你知道为什么吗 ?这是从 C 继承的东西,并且与线程一起工作我们需要一个指针吗?


编辑:并且在 Objective-C 中我们不能直接创建指针。如果我想用指针做类似下面的事情,这是否有效? (我知道 NSNumber 和 NSValue 类,但我只想了解它是如何工作的)

int i = 5;
int *pointer_i = &i;
[self performSelector:mySelector withObject:pointer_i];

最佳答案

你只能给出指针作为参数,所以 Objective-C 对象。如果您想使用整数,请尝试以下操作:

int i;
[self performSelector:@selector(mySelector:) withObject:[NSNumber numberWithInt:i]];

关于iphone - 为什么执行选择器:withObject: methods can only take id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5378473/

相关文章:

iphone - 用户当前语言环境的货币符号

ios - iPhone 6 plus 的@3x Assets 可以有奇数像素吗?即 248 x 171 像素

objective-c - Box2D 动态体卡住(iPhone)

ios - NSTimer无法正确暂停和恢复

c++ - 从 USB 读取 rfid 标签

创建一个动态数组并初始化它

iphone - UIScrollView 和 setContentOffset

iphone - 通过 Windows VPN 的 Bonjour

iphone - 在 Singleton 中设置 NSMutableDictionary 条目?

c - 宏定义函数