I'm writing a Button class in Objective-C with

2019-01-23 12:56发布

I'm writing a simple button class, something like this:

@interface MyButton : NSObject {
  id object;
  SEL action;
}
@property(strong) id object;
@property SEL action;
-(void)fire;
@end


@implementation MyButton

@synthesize object, action;

-(void)fire {
  [object performSelector:action];
}

@end

I get the following warning from Clang on [object performSelector:action]:

PerformSelector may cause a leak because its selector is unknown

After some research I see that selectors can belong to families which have different memory requirements. The intention is for the action to return void, so it shouldn't cause any ARC difficulties and should fit in the none family.

It looks like the relevant piece of preprocessor code I want is, or is a variant of:

__attribute__((objc_method_family(none)))

But where do I put that to tell Clang not to worry?

4条回答
Summer. ? 凉城
2楼-- · 2019-01-23 13:28

If you're writing new code, the best way to handle callbacks is to use blocks; they are both safer and more flexible than performSelector. See http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html .

查看更多
爷、活的狠高调
3楼-- · 2019-01-23 13:29

In the LLVM 3.0 compiler in Xcode 4.2 you can suppress the warning as follows:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [object performSelector:action];
#pragma clang diagnostic pop

Thanks to Scott Thompson (about this similar question: performSelector may cause a leak because its selector is unknown) for the answer.

查看更多
▲ chillily
4楼-- · 2019-01-23 13:33

I use this:

[object tryToPerform:action with:nil];
查看更多
老娘就宠你
5楼-- · 2019-01-23 13:35

Because you're dynamically assigning action, the compiler sees a possible leak with ARC. In the future, the LLVM compiler may allow you to suppress the warning. Until then, you can avoid the warning by using the runtime's objc_msgSend() instead of -performSelector:.

First, import the runtime message header

#import <objc/message.h>
Next, replace performSelector: with objc_msgSend()

    // [object performSelector:action];
    objc_msgSend(object, action);
查看更多
登录 后发表回答