我想将NSInteger参数传递给使用performSelector调用的方法:WithObject:afterDelay,所以我有这段代码:
id arg = [NSNumber numberWithInt:myIdentifier];
[self performSelector:@selector(myMethod:) withObject:arg afterDelay:kDuration];
- (void) myMethod: (id) identifier
{
...
}
在myMethod中:,如何将标识符从id转换为NSInteger?
我之前看过这个问题: SEL performSelector and arguments
但我不明白myMethodForNumber: - 如何用来取消编号?
答案 0 :(得分:2)
myMethod
可以接受NSNumber
作为参数。然后,您可以使用NSNumber
实例方法获取整数值。
- (void)myMethod:(NSNumber *)number {
NSInteger value = [number integerValue];
}
答案 1 :(得分:0)
如果您知道其类型,则可以转换该值:
- (void) myMethod: (id)identifier
{
NSNumber *myNumber = (NSNumber *)identifier;
}
或者,您只需指定方法所采用的类型:
- (void) myMethod:(NSNumber *)myNumber
{
// Use myNumber
}
答案 2 :(得分:0)
更新:请参考the question
使用performSelector:
进行读写如下,
@import os.log;
- (void)testSEL
{
os_log(OS_LOG_DEFAULT, "Original NSObject.version is %zd", NSObject.version); // original 0
NSObject.version = 1;
os_log(OS_LOG_DEFAULT, "Custom NSObject.version is %zd (should be 1)", NSObject.version); // custom 1
// Reading
NSInteger v = (NSInteger)[NSObject performSelector:@selector(version)];
os_log(OS_LOG_DEFAULT, "Reading: version is %zd", v);
// Writing
[NSObject performSelector:@selector(setVersion:) withObject:(__bridge id)(void *)2];
os_log(OS_LOG_DEFAULT, "Writing: NSObject.version is %zd", NSObject.version);
}