简体   繁体   中英

Undefined selector with Objective-C runtime - blocks

I'm trying to create a function where multiple times I do the same thing. I've deceided to go with a block this time. However after writing following code:

- (BOOL)readyForProcessing {
    void (^notDeclaredError)(id) = ^(id missingObject) {
        NSString *missingObjectName = NSStringFromSelector(@selector(missingObject));
        NSString *errorDescription = [NSString stringWithFormat:@"You need to provide %@ property", missingObjectName];
        [self configureErrorWithLocalizedDescription:errorDescription];
    };

    if (!self.delegate) notDeclaredError(self.delegate);

    return (self.error == nil);
}

I get a warning in the line, where I declare missingObjectName .

 Undeclared selector 'missingObject'

I know it will probably try to make a NSString from missingObject instead of delegate . How to pass it this way, that the output will be delegate and the code will be inside the block?

Here you are:

- (BOOL)readyForProcessing {
    void (^notDeclaredError)(SEL) = ^(SEL missingObject) {
        NSString *missingObjectName = NSStringFromSelector(missingObject);
        NSString *errorDescription = [NSString stringWithFormat:@"You need to provide %@ property", missingObjectName];
        [self configureErrorWithLocalizedDescription:errorDescription];
    };

    if (!self.delegate) notDeclaredError(@selector(delegate));

    return self.error ? NO : YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM