Accessing private variable in Category results in linker error

后端 未结 2 1467
半阙折子戏
半阙折子戏 2020-12-30 09:51

EDIT: I\'m not going to do this, I now realize how dangerous this can be. But, the question stays for purely academic purposes.

I\'m trying to implement a category o

2条回答
  •  天命终不由人
    2020-12-30 10:07

    You shouldn't really, but access it like a pointer to a member of a struct:

    -(NSMutableArray *)displayedItems {
      return self->_displayedItems;
    }
    

    This is a fragile thing to do, as I'm sure you're aware however ;)

    UPDATE: Since you've mentioned the above doesn't work, try dropping down to the runtime:

    -(NSMutableArray *)displayedItems {
            NSMutableArray *displayedItems;
            object_getInstanceVariable(self, "_displayedItems", (void *)&displayedItems);
            return displayedItems;
    }
    

    (Tested, works)

提交回复
热议问题