首页 > 代码库 > 类名与字符串的互转

类名与字符串的互转

 

类名转类

id class = NSClassFromString([NSString stringWithFormat:@"%@Controller", self.viewName]);

 

类转类名

NSString *className = NSStringFromClass([UIView class]);

 

其他转换

FOUNDATION_EXPORT NSString *NSStringFromSelector(SEL aSelector);  //SEL转字符串
FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName);  //字符串转SEL

FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) NS_AVAILABLE(10_5, 2_0);  //协议转字符串
FOUNDATION_EXPORT Protocol * __nullable NSProtocolFromString(NSString *namestr) NS_AVAILABLE(10_5, 2_0);  //字符串转协议

 

类名与字符串的互转