Search code examples
iosobjective-cmethodspropertiesprivate

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?


Solution

  • 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