为什么用我的performSelector警告 [英] why warning with my performSelector

查看:95
本文介绍了为什么用我的performSelector警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个简单的PerformSelector,它向obj发送一条消息以执行循环方法.一切正常,但我收到以下黄色警告.

Below is a simple PerformSelector that sends a message to obj to perform the looping method. All works well but I get the following yellow warning.

PerformSelector可能会导致泄漏,因为其选择器未知.

PerformSelector may cause a leak because its selector is unknown.

#import "MyClass.h"
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        MyClass *obj = [[MyClass alloc]init];

        SEL mySel = @selector(looping);
        [obj performSelector:mySel];
    }
    return 0;
}

此警告没有意义,因为performSelector必须知道mySel,因为确实调用了循环方法-任何想法都发生了什么??

This warning does not make sense because the performSelector must be aware of mySel because the looping method does get called - any ideas whats going on ??

更新

MyClass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject

-(void)looping;

@end

MyClass.m

#import "MyClass.h"

@implementation MyClass

-(void)looping{

    NSLog(@"Hey, i'm looping");

}

@end

推荐答案

更新-真实答案

这是特定于ARC的:

performSelector可能会导致泄漏,因为它的选择器是未知

简而言之,ARC使用基于命名约定和绑定到选择器的任何其他属性的信息.当通过名称访问选择器并通过performSelector:系列方法执行选择器时,该信息将丢失,并且编译器警告您,由于该信息被剥夺,因此必须对引用计数进行一些假设.

In short, ARC uses information based on the naming conventions and any additional attributes bound to the selector. When accessing a selector by name and performing it via the performSelector: family of methods, that information is lost and the compiler is warning you that it must make some assumptions regarding reference counting because this information is stripped away.

简而言之,您发布的特定程序是安全的,但是建议您使用对ARC友好的替代程序.

In short, the specific program you posted is safe but you are encouraged to use an alternative which is ARC-friendly.

上次答复

选择器的声明不需要对当前翻译可见才能调用.

A selector's declaration does not need to be visible to the current translation in order to call it.

允许编译器假定参数的默认类型以及类和实例方法的返回类型(id是默认值).

The compiler is allowed to assume default types for parameters and return types for class and instance methods (id is the default).

有一些编译器警告可以警告您这些不可靠的操作.

There are several compiler warnings which can warn you of these shady actions.

您可能忘记了在@interface中声明选择器looping,或者如果它带有参数,则可能省略了冒号:looping:是其名称.

You probably forgot to declare the selector looping in the @interface, or you may have omitted the colon, if it has arguments: looping: would be its name.

这篇关于为什么用我的performSelector警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆