將Type轉換成字串的extension

extension String {
    
    init<T>(type: T.Type) {
        self = "\(type.self)"
    }
    
    init<T>(type: T) {
        self = "\(type(of: type))"
    }
}

 

使用

        let id = String(type: ViewController2.self)
        let id2 = String(type: self)

        guard let viewController2 = storyboard?.instantiateViewController(withIdentifier: id) else {
            return
        }

Output: "ViewController2"

Output: "ViewController"

arrow
arrow
    全站熱搜

    小賢 發表在 痞客邦 留言(0) 人氣()