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

I have a block that is stored as an instance variable in a class

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

and I want xcode to automatically fill the block, as when entering [UIView animateWithDuration and xcode autocompletes block for it.

When I autofill my block, it just fills

 [self.suggestedSearchTermView setWordSelected:(didSelectWord)wordSelected 

instead

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

Is it possible to change something so that Xcode understands how to autofill this block?

+4
source share
3 answers

Ok, I did some tests.

Apparently, you have two (far from ideal) options:

  • avoid typedef and declare a property as

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

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

  • explicitly add setter declaration in interface

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

    this will cause Xcode to allow type determination before the installer is defined, which will give you the nice autocomplete that you expect. The obvious drawback is the extra setter declaration in the interface.

However, you must fill out a bug report: http://openradar.appspot.com/

+10
source

Declare your property without a typedef , for example:

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

With this definition, Xcode will give you the extension below:

 MyClass *test = [MyClass new]; [test setWordSelected:(void (^)(NSString *))wordSelected]; 
+2
source

In heightened disappointment, I made a macro consolidating this crude process.

 #define BlockProperty(SIGNATURE,TYPENAME,varname,Varname) typedef SIGNATURE; @property (nonatomic,copy) TYPENAME varname; - (void) set##Varname:(TYPENAME)_ 

Now, what would be required before (for correct autocomplete).

 typedef void(^OnEvent)(BOOL ok,id result); @property (nonatomic,copy) OnEvent varname; - (void) setVarname:(OnEvent)_; 

just

 BlockProperty(void(^OnEvent)(BOOL ok, id result),OnEvent,varname,VarName); 

QUITE is a bit simpler, less verbose, and you benefit from typedef AND, and you don't need to create an unsightly theoretical unnecessary setter declaration!

If you want to reuse the “type”, you will need another (which this time will only accept three parameters (since the block type cannot be updated).

 #define BlockProp(TYPENAME,varname,Varname) @property (nonatomic,copy) TYPENAME varname; - (void) set##Varname:(TYPENAME)_ BlockProp(OnEvent,anotherVar,AnotherVar); 

You can simply create a new block type (name) for each property, even if their signatures match (using the first macro), but this kind of gross. Enjoy it!

+1
source

Source: https://habr.com/ru/post/1499359/


All Articles