1.swift 版本
static public func getClassFromString(_ classString: String) -> AnyClass? {
guard let bundleName: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String else {
return nil
}
let classPath = bundleName + "." + classString;
var anyClass: AnyClass? = NSClassFromString(classPath)
if (anyClass == nil) {
anyClass = NSClassFromString(classString)
}
return anyClass
}
2.OC 版本
+(Class)getClassFromString:(NSString *)classString{
Class anyClass = NSClassFromString(classString);
if(anyClass == nil){
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *bundleName = [infoDict objectForKey:@"CFBundleName"];
NSString *classPath = [NSString stringWithFormat:@"%@.%@",bundleName,classString];
anyClass = NSClassFromString(classPath);
}
return anyClass;
}
网友评论