大佬教程收集整理的这篇文章主要介绍了Swift语言NSClassFromString,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
请注意,Swift类现在已命名为名称空间,因此它将是“ Appname.MyVIEwController”而不是“ MyVIEwController”
多亏了EDWin Vermeer的回答,我能够通过执行以下操作来构建一些东西来将Swift类实例化为Obj-C类:
@H_419_10@// swift file
// extend the NSObject class
extension NSObject {
// create a static method to get a swift class for a String name
class func swiftClassFromString(classname: String) -> AnyClass! {
// get the project name
if var appname: String? = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundlename") as String? {
// generate the full name of your class (take a look into your "YourProject-swift.h" filE)
let classStringname = "_TtC\(appname!.utf16count)\(appName)\(countElements(className))\(className)"
// return the class!
return NSClassFromString(classStringName)
}
return nil;
}
}
// Obj-C file
#import "YourProject-Swift.h"
- (voID)aMethod {
Class class = NSClassFromString(key);
if (!class)
class = [NSObject swiftClassFromString:(key)];
// do something with the class
}
您也可以在纯Obj-C中执行此操作:
@H_419_10@- (Class)swiftClassFromString:(NsString *)classname {
NsString *appname = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundlename"];
NsString *classStringname = [NsString StringWithFormat:@"_TtC%d%@%d%@", appname.length, appname, classname.length, classname];
return NSClassFromString(classStringName);
}
我希望这会对某人有所帮助!
以上是大佬教程为你收集整理的Swift语言NSClassFromString全部内容,希望文章能够帮你解决Swift语言NSClassFromString所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。