【问题标题】:Objective-C - NSTimer: unrecognized selector sent to class - call method from another classObjective-C - NSTimer:发送到类的无法识别的选择器 - 从另一个类调用方法
【发布时间】:2014-08-21 10:46:01
【问题描述】:

addCloudOne 是 Food 类中的一个方法。以下代码会导致崩溃并出现以下错误:

+[Food addCloudOne]:无法识别的选择器发送到类 0x1000ad760

    SEL selector = @selector(addCloudOne);
    [NSTimer scheduledTimerWithTimeInterval:k1 target:[Food class] selector:selector userInfo:nil repeats:YES];

你有什么想法吗?

【问题讨论】:

  • 你应该给instance参考而不是class作为目标addCloudOne可能是instance method

标签: objective-c selector nstimer


【解决方案1】:

需要指定Food类的实例,所以这个说法不正确:

target:[Food class]

所以您传递给NSTimer 的是Class 对象,而不是Food 对象。

相反,您可能需要Food 实例的实例变量并指定:

@interface MyClass ()
{
    Food *_food;
}

@implementation MyClass
...
- (void)whatever
{
    _food = [Food new];             // This might need to be elsewhere
    SEL selector = @selector(addCloudOne);
    [NSTimer scheduledTimerWithTimeInterval:k1
                                     target:_food
                                   selector:selector
                                   userInfo:nil
                                    repeats:YES];
}

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多