Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the correct autocomplete in XCode for a block variable?

I have a block thats stored as an instance variable in a class

typedef void ((^didSelectWord)(NSString* word));
@property (nonatomic,strong) didSelectWord wordSelected;

and i want xcode to auto fillout the block like when you type [UIView animateWithDuration and xcode autocompletes a block for it.

When i autocomplete my block it just fills out

[self.suggestedSearchTermView setWordSelected:(didSelectWord)wordSelected

instead of

[self.suggestedSearchTermView setWordSelected:^(NSString *word) {

Is it possible to change something to make Xcode understand how to autocomplete this block?

like image 972
bogen Avatar asked Dec 11 '22 11:12

bogen


2 Answers

Ok I did some testing.

Apparently you have two (far from perfect) options:

  1. avoid the typedef and declare the property as

    @property (nonatomic,strong) void (^wordSelected)(NSString * word);
    

    As noted in the comments, this has the drawback of skipping the parameter name in the autocompletion.

  2. explicitly add a setter declaration in the interface

    typedef void ((^DidSelectWordBlock)(NSString* word));
    
    @interface YourClass : NSObject
    
    @property (nonatomic,strong) DidSelectWordBlock wordSelected;
    - (void)setWordSelected:(DidSelectWordBlock)wordSelected;
    
    @end
    

    this will cause Xcode to resolve the type definition before the setter definition, giving you the nice autocompletion that you would expect. The obvious drawback is the extra setter declaration in the interface.

That said, you should fill in a bug report: http://openradar.appspot.com/

like image 124
Gabriele Petronella Avatar answered Jan 13 '23 01:01

Gabriele Petronella


Declare your property without typedef, like this:

@property (nonatomic,strong) void (^wordSelected)(NSString *word);

With this definition Xcode would give you the expansion below:

MyClass *test = [MyClass new];
[test setWordSelected:(void (^)(NSString *))wordSelected];
like image 41
Sergey Kalinichenko Avatar answered Jan 13 '23 02:01

Sergey Kalinichenko