How to I pass @selector as a parameter?

后端 未结 5 1034
[愿得一人]
[愿得一人] 2021-01-30 07:00

For the method:

[NSThread detachNewThreadSelector:@selector(method:) toTarget:self withObject:(id)SELECTOR];

How do I pass in a @selector? I tr

相关标签:
5条回答
  • 2021-01-30 07:05

    You can convert between selectors and string objects using the NSStringFromSelector() and NSSelectorFromString() functions. So you can just pass string objects instead.

    Alternately, if you don't want to change your methods, you can create an NSInvocation to create an invocation for your method call (since it can set up invocations with non-object arguments), and then to call it do [NSThread detachNewThreadSelector:@selector(invoke) toTarget:myInvocation withObject:nil];

    0 讨论(0)
  • 2021-01-30 07:05

    Please see: passing a method as an argument

    0 讨论(0)
  • 2021-01-30 07:07

    Use NSValue, like so:

    +(void)method1:(SEL)selector {
        NSValue *selectorValue = [NSValue value:&selector withObjCType:@encode(SEL)];
        [NSThread detachNewThreadSelector:@selector(method2:) 
                                 toTarget:self 
                               withObject:selectorValue];
    }
    

    NSValue is intended as an object-wrapper for arbitrary non-object types.

    0 讨论(0)
  • 2021-01-30 07:12

    If you don't want to specify an object just use nil.

    [NSThread detachNewThreadSelector:@selector(method:) toTarget:self withObject:nil];
    

    If you need to pass an object to the selector it would look something like this.

    Here I'm passing a string to the method "setText".

    NSString *string = @"hello world!";
     [NSThread detachNewThreadSelector:@selector(setText:) toTarget:self withObject:string];
    
    
    -(void)setText:(NSString *)string {
        [UITextField setText:string]; 
    }
    
    0 讨论(0)
  • 2021-01-30 07:19

    The problem here isn't passing a selector to a method, per se, but passing a selector where an object is expected. To pass a non-object value as an object, you can use NSValue. In this case, you'll need to create a method that accepts an NSValue and retrieves the appropriate selector. Here's an example implementation:

    @implementation Thing
    - (void)method:(SEL)selector {
        // Do something
    }
    
    - (void)methodWithSelectorValue:(NSValue *)value {
        SEL selector;
    
        // Guard against buffer overflow
        if (strcmp([value objCType], @encode(SEL)) == 0) {
            [value getValue:&selector];
            [self method:selector];
        }
    }
    
    - (void)otherMethodShownInYourExample {
        SEL selector = @selector(something);
        NSValue *selectorAsValue = [NSValue valueWithBytes:&selector objCType:@encode(SEL)];
        [NSThread detachNewThreadSelector:@selector(methodWithSelectorValue:) toTarget:self withObject:selectorAsValue];
    }
    @end
    
    0 讨论(0)
提交回复
热议问题