從 Objective-C 遷移到 Swift 時,獲取類名字符串的寫法有所變化,下面對比不同版本的實現:
String(LXFContactCell)
String(describing: LXFContactCell.self)
實際應用場景,比如註冊 Nib:
tableView.register(UINib(nibName: String(describing: LXFContactCell.self), bundle: nil), forCellReuseIdentifier: "LXFContactCellID")
可以看到,Swift 3 通過 String(describing:) 並傳入 Type.self 的方式來獲取類名,寫法更明確也更安全。