简体   繁体   中英

Objective-c, method declaration in .h and private property in .m confusion

I am reading a this tutorial in which he declared a method in cell .h file which accept a block but did not implement the method in .m class , he declared a private property with same name as method @property (copy, nonatomic) void (^didTapButtonBlock)(id sender);

what is this practice? only declaring a method in .h and making a private property in .m

I tried to do it simply like this

I created a method in .h file

-(void)xyz:(NSString*)string;

in .m file

@property (nonatomic, strong) NSString *string;

But Xcode giving warning Method definition for 'xyz' not found

Kindly tell what is going behind the scene?

He's exposing the setter method for the block variable, but keeping the getter private, if you notice, the method have the word set , which is the setter method for a property

This is how you can do the same:

-(void)setXyz:(NSString*)xyz;

and in .m:

@property (nonatomic, strong) NSString *xyz;

This way is to make sure other class cannot get the property instance, but can give it value

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