I read inline documentation of Swift and I am bit confused.
1) Any
is a protocol that all types implicitly conform.
2) AnyObject
is a protocol to which all classes implicitly conform.
3) Int
, Float
, Double
are structs
Here is a sample code:
import UIKit
func passAnyObject(param: AnyObject) {
print(param)
}
class MyClass {}
struct MyStruct {}
let a: Int = 1
let b = 2.0
let c = NSObject()
let d = MyClass()
let e = MyStruct()
passAnyObject(a)
passAnyObject(b)
passAnyObject(c)
passAnyObject(d)
//passAnyObject(e) // Argument type 'MyStruct' does not conform to expected type 'AnyObject'
if a is AnyObject { // b, d, e is also AnyObject
print("(a.dynamicType) is AnyObject")
}
What I don't understand is why Int
, Double
, Float
are AnyObject
s? Why compiler doesn't say anything? Those types are declared as structs. Struct MyStruct
cannot be passed to the method on the top because it does not conform to AnyObject
.
Could you help me understand why Int
, Double
and Float
are AnyObject
or why compiler thinks they are?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…