Translate

2016年5月6日 星期五

事件用字串來呼叫


#include <objc/message.h>

- (IBAction)buttonevent:(id)sender {

NSDictionary *itemDic = [[NSDictionary alloc]init];

  SEL s = NSSelectorFromString(@"eventname:");
        if ([self respondsToSelector:s]) {
            ((id (*)(id, SEL, NSDictionary*))objc_msgSend)(self, s, itemDic);
        }

}

- (void) eventname:(id)obj
{
    //do something...
}

nsarray有包含字串

 NSArray *ar=@[@"john",@"bally",@"bill"];
 NSString *check=@"john";
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", ar];

    BOOL result = [predicate evaluateWithObject:check];