【问题标题】:Unable to read property value in extension无法读取扩展中的属性值
【发布时间】:2017-02-17 21:28:24
【问题描述】:

我已经读到了

类扩展还专门设计为允许属性公开readonly 和私下readwrite

所以我创建了一个类abc,代码如下:

 @interface abc ()

 @property (strong,nonatomic) NSString *sampleString;

 @end

 @implementation abc


  @end

我想在abc的另一个类子类中访问sampleString

我创建了一个pqr 的类abc 的子类

pqr 中,我正在尝试访问sampleStr,但无法做到这一点。

 @implementation cccc

  - (void)accessPrivateMember
 {
   self.sampleStr ; //Not able to acces

 } 

 @end

我的方向错了吗?

【问题讨论】:

  • 因为sampleString不在类的公共接口中。在你的类的.h 文件中声明这个属性。
  • 在 abc.h 中添加只读属性为 @property (strong,nonatomic, readonly) NSString *sampleString;

标签: ios objective-c oop extension-methods


【解决方案1】:

您应该在 abc 的“.h”文件中公开此属性。添加这个:

@property (strong,nonatomic) NSString *sampleString;

【讨论】:

    【解决方案2】:

    您应该将属性放在头文件 (.h) 中。在您的情况下,sampleString 是私有财产,因为它位于实现文件(.m)中。另外,如果你想访问私有财产,你可以使用self.valueForKey("sampleStr")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 2022-06-28
      相关资源
      最近更新 更多