【问题标题】:Objective-c, method declaration in .h and private property in .m confusionObjective-c,.h 中的方法声明和 .m 中的私有属性混淆
【发布时间】:2018-04-26 02:06:00
【问题描述】:

我正在阅读this 教程,其中他在cell .h file 中声明了一个接受block 的方法,但没有.m class 中实现该方法,他声明了@987654325 @与method同名@property (copy, nonatomic) void (^didTapButtonBlock)(id sender);

这是什么做法?只在 .h 中声明一个方法并在 .m 中创建一个私有属性

我试着这样做

我在 .h 文件中创建了一个方法

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

在 .m 文件中

@property (nonatomic, strong) NSString *string;

但是 Xcode 给出警告Method definition for 'xyz' not found

请告诉幕后发生了什么?

【问题讨论】:

    标签: ios objective-c methods properties private


    【解决方案1】:

    他公开了块变量的setter方法,但保持getter私有,如果你注意到,该方法有set这个词,这是一个属性的setter方法

    你可以这样做:

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

    在 .m 中:

    @property (nonatomic, strong) NSString *xyz;
    

    这种方式是为了确保其他类无法获取属性实例,但可以给它赋值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-09
      • 2022-01-13
      • 2011-08-04
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      相关资源
      最近更新 更多