相信码农们都听说过反射这个词,今天要说的iOS下的反射使用方法。虽然在Object-C和Swift中都使用NSClassFromString这个方法,但是在使用过程中还是有细微的差别。具体看下面的代码注释吧
- Object-C中使用
Class urlClass = NSClassFromString(controllerValue); //controllerValue为待反射的类名
id toController = [[urlClass alloc] initWithNibName:controllerValue bundle:nil];
if ([toController isKindOfClass:[BaseViewController class]]) {
UIViewController *currentController = [self getCurrentViewController];
((BaseViewController *)toController).pushNotification = self.pushNotice;
[currentController.navigationController pushViewController:toController animated:YES];
}
Object-C中NSClassFromString调用时传入的参数就是直接的类名,相信在Object-C中使用过该方法的人,都会用,非常简单
- Swift中使用
if let appName: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleNam