簡體   English   中英

Swift比較兩個MetaType?

[英]Swift compare two MetaTypes?

我需要測試以查看我傳遞給函數的Type是否屬於某個類

func doSomething<T> (type: T.Type) -> T {

   // Check to see if type is the same as Raw type?

   if type == Raw.self { } // Doesn't work, Compile error

   if type is? Raw.self { } // Doesn't work, Compile error
}

@objc class Raw {

}

函數重載將是我的首選,但要回答這個問題。 使用NSStringFromClass或以下代碼:

@objc class Raw {

}

func doSomething<T: AnyObject> (type: T.Type) -> T {

    type as AnyClass === Raw.self as AnyClass { }

    // return something
}

這個解決方案似乎也適用於純Swift類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM