I have a superclass called City and a bunch of subclasses that inherit from this class. For example London and NewYork. Each of these subclasses have their own unique methods and instance variables.
When my app launches I geocode the current location and instantiate the subclass depending on which city I’m currently in.
Right now I have multiple properties defined to access these classes, so
@property (nonatomic, strong) London *london;
@property (nonatomic, strong) NewYork *newYork;
...
...
I will only ever need one object of this type in existence at any one time so would only like one property defined to access this object. Something like
@property (nonatomic, strong) City *city;
Is there a way I can dynamically create the subclass I require without having to declare multiple properties?
You can definitely implement subclasses in Objective-C. You could do the following to instantiate a city:
To call city-specific methods:
The methods “isKindOfClass:”, “isMemberOfClass:”, “conformsToProtocol:” and “respondsToSelector:” are useful class methods for you to get familiar with.