美文网首页
Swift Tips

Swift Tips

作者: 南国青天 | 来源:发表于2016-04-08 10:47 被阅读55次

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!
    }
}

相关文章

网友评论

      本文标题:Swift Tips

      本文链接:https://www.haomeiwen.com/subject/jkjvlttx.html