iOS 獲取類的字符串名稱 Swift4


 

 

以下實例基於Swift4,且在class, struct, enum中都可用:
 
 
class Foo {

    // 實例屬性中指定明確的類名來獲取名稱
    var typeName: String {
        return String(describing: Foo.self)
    }

    // 實例屬性中動態獲取類名來獲取名稱
    var otherTypeName: String {
        let thisType = type(of: self)
        return String(describing: thisType)
    }

    // 類屬性中直接獲取名稱
    static var typeName: String {
        return String(describing: self)
    }

}

Foo().typeName       // = "Foo"
Foo().otherTypeName  // = "Foo"
Foo.typeName         // = "Foo"

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM