简体   繁体   English

Objective-C,.h中的方法声明和.m中的私有属性

[英]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); 我正在阅读教程,其中他在cell .h file声明了一个接受block的方法, 但未.m class实现该方法,他声明了与method @property (copy, nonatomic) void (^didTapButtonBlock)(id sender);同名的private property @property (copy, nonatomic) void (^didTapButtonBlock)(id sender);

what is this practice? 这是什么做法? only declaring a method in .h and making a private property in .m 只在.h中声明方法并在.m中设置私有属性

I tried to do it simply like this 我试图这样简单地做

I created a method in .h file 我在.h文件中创建了一个方法

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

in .m file 在.m文件中

@property (nonatomic, strong) NSString *string;

But Xcode giving warning Method definition for 'xyz' not found 但是Method definition for 'xyz' not found警告Method definition for 'xyz' not found Xcode

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 他公开了block变量的setter方法,但是将getter set私有,如果您注意到的话,该方法包含单词set ,这是属性的setter方法。

This is how you can do the same: 这是您可以这样做的方法:

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

and in .m: 并在.m中:

@property (nonatomic, strong) NSString *xyz;

This way is to make sure other class cannot get the property instance, but can give it value 这种方法是确保其他类无法获取属性实例,但可以为其赋值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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