美文网首页
Swift中获取class名字

Swift中获取class名字

作者: 绍清_shao | 来源:发表于2019-08-19 14:49 被阅读0次

ViewController.swift中

    @IBAction func Tap(_ sender: Any) {
        
        /* 返回内部类名 */ 
        print("object_getClassName: \(object_getClassName(self))")
        //打印:object_getClassName: 0x0000600002b8c680        

        /* 返回应用程序名+类名 */ 
        print("NSStringFromClass: \(NSStringFromClass(type(of: self)))")
        //打印:NSStringFromClass: ViewTest.ViewController
 
        /* 返回应用程序名+类名,并去掉应用程序名 */
        print("NSStringFromClass: \(NSStringFromClass(type(of: self)).components(separatedBy: ".").last!)")
        //打印:NSStringFromClass: ViewTest.ViewController

        /* 返回应用程序名+类名+内存地址 */ 
        print("self: \(self)")
        
        /* 返回应用程序名+类名+内存地址 */ 
        print("description: \(self.description)")
        
        /* 返回类名 */ 
        print("type: \(type(of: self))")
        
    }

打印结果


NSStringFromClass: ViewTest.ViewController
NSStringFromClass: ViewController
self: <ViewTest.ViewController: 0x7f8b67d12500>
description: <ViewTest.ViewController: 0x7f8b67d12500>
type: ViewController

相关文章

网友评论

      本文标题:Swift中获取class名字

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