I have several data sources that I use for one UIViewController. The My view controller uses KeyValue Observing to track the state of certain properties at runtime. When I replace dataSources, I need to stop observing these properties. The problem is that I'm not sure about the dataSource class at runtime, so something like this is not valid:
if (aDataSource != dataSource) {
[dataSource removeObserver:self forKeyPath:@"someKeyPath"];
[dataSource release];
dataSource = [aDataSource retain];
}
The compiler needs a specific class to know the interface of the object. How can I grab the dataSource class in this particular case and then the typcast dataSource for removeObserver: forKeyPath: selector above? I prefer something dynamic / smarter than caching the class name in an NSString instance and referring to this every time I switch. Meaning, I could always do something like:
NSString *lastDataSource = @"MyClass";
Class foo = [NSClassFromString(lastDataSource)];
Thank.
anon
source
share