iosswiftobjective-cxcodelanguage-interoperability

Protected variable in an objective c super class is inaccessible in a swift subclass


@interface MyObjectiveCmainClass
{
@protected
    NSMutableArray* thisStringIsInaccessibleInSwiftSubclasses;//this I can't access in swift subclass
}
@property NSString* thisStringIsAccessibleInSwiftSubclasses;//this I can access in swift subclass
- (void) thisMethodIsAccessibleInSwiftSubclasses;//this too I can access in Swift subclass
@end
class MySwiftSubclass : MyObjectiveCmainClass {
    
      override func thisMethodIsAccessibleInSwiftSubclasses() {
         NSLog(self.thisStringIsAccessibleInSwiftSubclasses)
    }
}

Any Idea why I can't access the @protected attribute on my ObjectiveC instance variable?


Solution

  • As Alexander Explained in his answer, this is not "straightforwardly" achievable.

    Since I'm not willing to alter the objective C super class, I decided to depend on KVC to achieve what I need.

     (value(forKey: "thisStringIsInaccessibleInSwiftSubclasses") as! NSString)//you now have access to the @protected instance variable 
    

    I'll then create a main swift class and do all the KVC work in it.