Using instance variables with Modern Runtime

前端 未结 5 1800
悲哀的现实
悲哀的现实 2020-12-14 23:43

I have several years of experience in Obj-c and Cocoa, but am just now getting back into it and the advances of Obj-C 2.0 etc.

I\'m trying to get my head around the

5条回答
  •  甜味超标
    2020-12-15 00:06

    I am running into the same problem. The way I am working around not being able to access the synthesized instance variables is the following:

    public header

    @interface MyObject:NSObject {
    }
    @property (retain) id instanceVar;
    @property (retain) id customizedVar;
    @end
    

    private header / implementation

    @interface MyObject()
    @property (retain) id storedCustomizedVar;
    @end
    
    @implementation MyObject
    @synthesize instanceVar, storedCustomizedVar;
    @dynamic customizedVar;
    
    - customizedVar {
      if(!self.storedCustomizedVar) {
        id newCustomizedVar;
        //... do something
        self.storedCustomizedVar= newCustomizedVar;
      }
      return self.storedCustomizedVar;
    }
    
    - (void) setCustomizedVar:aVar {
      self.storedCustomizedVar=aVar;
    }
    
    @end
    

    It's not that elegant, but at least it keeps my public header file clean.

    If you use KVO you need to define customizedVar as dependent key of storedCustomizedVar.

提交回复
热议问题