2016-04-28 56 views
0

在Swift中可以這樣做嗎?是否有可能像C#中一樣檢查Swift中的類型?

if (obj1.GetType() == obj2.GetType()) { /* do something */} 
+0

'如果OBJ1是obj2.dynamicType'? – jtbandes

+0

@jtbandes不幸的是,它不能編譯這段代碼 – mra214

+0

你可以通過if(obj1 === obj2)來檢查objective1是否等於object2,如果這就是你正在尋找的。 –

回答

1

在這個問題 How do you find out the type of an object (in Swift)?
下面的幾個例子中提到,您可以使用鏡像結構:

let var1 = "Test" 
let var2 = "Hello World" 
let var3 = UIView() 
let var4 = UIView(frame: CGRect.zero) 

let mirror1 = Mirror(reflecting: var1) 
let mirror2 = Mirror(reflecting: var2) 
let mirror3 = Mirror(reflecting: var3) 
let mirror4 = Mirror(reflecting: var4) 

print(mirror1.subjectType == mirror2.subjectType) // true 
print(mirror2.subjectType == mirror3.subjectType) // false 
print(mirror3.subjectType == mirror4.subjectType) // true 
相關問題