Swift Tips
程序员文章站
2022-07-02 19:18:56
...
I have used Swift language to program a few month, get some trouble also get some fun. So I want write some tips in proccess of leaning.
1. Swift how to get elegant class name string:
We always like use the NSStringFromClass
in Objective-C Program. Maybe like below
@property (nonatomic) ExampleCell *cell
NSString *classString = NSStringFromClass([cell class])
//print "ExampleCell"
NSStringFromClass
is no longer useful in Swift. because the string contain "." to separate project name and class name. you have to write this kind of extension. it look like a lot verbose.
public extension NSObject{
public class var nameOfClass: String{
return "\(self)"
}
public var nameOfClass: String{
return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
}
}