Search code examples
iosobjective-csubclassinstance-variables

Expose a protected Objective-C instance variable to subclass


I am subclassing a pod's class, and in this class there's a private instance variable that I want to expose and use within my class:

@interface MySuperClass () {
    UIScrollView *_scrollView;
}

Usually with exposing a private member or method, I would use a category like someone previously mentioned here, but I am having a problem doing it with a private instance variable. I read here that Associative References might work, but I wasn't able to make it work.


Solution

  • Try implementing in child class:

    - (UIScrollView *)scrollView {
        return [self valueForKey:@"_scrollView"]
    }