Objective C — narrow instance variable types in subclasses?

后端 未结 4 624
无人共我
无人共我 2020-12-11 08:02

Is it possible to narrow the allowed type of an ivar in a subclass. Something like this:

@interface person: NSObject {
  NSArray *friendArray;
}

@interface         


        
4条回答
  •  醉酒成梦
    2020-12-11 08:21

    No, you can't redeclare ivars at all. However, you can make a new method based property without making a new ivar.

    @property (nonatomic, copy) NSMutableArray* mutableFriends;
    
    @implementation MutablePerson
    
    - (NSMutableArray*)mutableFriends {
      return (NSMutableArray*)friendArray;
    }
    
    - (void)setMutableFriends:(NSMutableArray*)friends {
      self.friendsArray = [friends mutableCopy];
    }
    
    @end
    

提交回复
热议问题