AnyObject
官方api中AnyObject的介紹分爲三方面內容,看完相信你會有所收穫
The protocol to which all classes implicitly conform. //隱含類型確認協議
You use AnyObject
when you need the flexibility of an untyped object or when you use bridged Objective-C methods and properties that return an untyped result. AnyObject
can be used as the concrete type for an instance of any class, class type, or class-only protocol.
當你需要靈活的類型或者當你調用返回未知類型結果的oc方法和屬性時,你需要使用AnyObject。
AnyObject可以作爲任何類,類的類型(type),或者是類協議的有型的類型來使用。
For example:
//例如
class FloatRef {
let value: Float
init(_ value: Float) {
self.value = value
}
}
let x = FloatRef(2.3)
let y: AnyObject = x
let z: AnyObject = FloatRef.self
AnyObject
can also be used as the concrete type for an instance of a type that bridges to an Objective-C class.
Many value types in Swift bridge to Objective-C counterparts, like String
and Int
.
AnyObject也可以被用作爲一個oc對象的有型的類型。swift中的許多值類型和oc裏面的是相通的,比如String
和 Int
。
let s: AnyObject = "This is a bridged string." as NSString
print(s is NSString)
// Prints "true"
let v: AnyObject = 100 as NSNumber
print(type(of: v))
// Prints "__NSCFNumber"
The flexible behavior of the AnyObject
protocol is similar to
Objective-C’s id
type. For this reason, imported Objective-C types
frequently use AnyObject
as the type for properties, method parameters,
and return values.
AnyObject協議的靈活性與oc中的id是類似的。由於這個原因,AnyObject被頻繁地作爲oc類的屬性、方法參數的類型來使用
Casting AnyObject Instances to a Known Type //拋出AnyObject對象給一個已知類型
Objects with a concrete type of AnyObject
maintain a specific dynamic type and can be cast to that type using one of the type-cast operators
(as
, as?
, or as!
).
This example uses the conditional downcast operator (as?
) to conditionally cast the s
constant declared above to an instance of Swift’s String
type.
固定的類型的AnyObject
對象具有一個具體的動態類型,並且可以使用(as
, as?
, or as!
)被拋向那個具體的類型。下面的例子使用(as?
)拋向上面聲明的Swift中String
類型的對象s
if let message = s as? String {
print("Successful cast to String: \(message)")
}
// Prints "Successful cast to String: This is a bridged string."
If you have prior knowledge that an AnyObject
instance has a particular
type, you can use the unconditional downcast operator (as!
). Performing
an invalid cast triggers a runtime error.
如果你事先知道AnyObject
對象的詳細類型,可以使用 (as!
)。執行無效的拋向操作會引起運行時錯誤。
let message = s as! String
print("Successful cast to String: \(message)")
// Prints "Successful cast to String: This is a bridged string."
let badCase = v as! String
// Runtime error
Casting is always safe in the context of a switch
statement.
但在switch
語句中拋向操作是不會引起運行時錯誤的。
let mixedArray: [AnyObject] = [s, v]
for object in mixedArray {
switch object {
case let x as String:
print("'\(x)' is a String")
default:
print("'\(object)' is not a String")
}
}
// Prints "'This is a bridged string.' is a String"
// Prints "'100' is not a String"
Accessing Objective-C Methods and Properties //訪問OC方法和屬性
When you use AnyObject
as a concrete type, you have at your disposal every @objc
method and property—that is, methods and properties imported from Objective-C or marked with the @objc
attribute. Because Swift can’t guarantee at compile time that these methods and properties are actually available on an AnyObject
instance’s underlying type, these @objc
symbols are available as implicitly unwrapped optional methods and properties, respectively.
當你使用 AnyObject
作爲一個有型的類型,你必須處理每一個帶 @objc
標記或者來自於oc的方法和屬性。因爲Swift不能保證 潛在類型的AnyObject
對象的這些方法和屬性是真正可用,帶有@objc
標記的方法和屬性會分別在暗中被解綁。
This example defines an IntegerRef
type with an @objc
method named
getIntegerValue()
.
這個例子定義了一個IntegerRef
,它有一個標記了@objc
方法 getIntegerValue()
class IntegerRef {
let value: Int
init(_ value: Int) {
self.value = value
}
@objc func getIntegerValue() -> Int {
return value
}
}
func getObject() -> AnyObject {
return IntegerRef(100)
}
let obj: AnyObject = getObject()
In the example, obj
has a static type of AnyObject
and a dynamic type of IntegerRef
. You can use optional chaining to call the @objc
method getIntegerValue()
on obj
safely. If you’re sure of the dynamic type of obj
, you can call getIntegerValue()
directly.
這個例子中 ocj有一個AnyObject靜態類型和一個IntegerRef動態類型。 你可以使用可選鏈通過 obj調用帶有 @objc標記的方法getIntegerValue()。如果你確定obj的動態類型,你可以直接調用。
let possibleValue = obj.getIntegerValue?()
print(possibleValue)
// Prints "Optional(100)"
let certainValue = obj.getIntegerValue()
print(certainValue)
// Prints "100"
If the dynamic type of obj
doesn’t implement a getIntegerValue()
method, the system returns a runtime error when you initialize
certainValue
.
Alternatively, if you need to test whether obj.getIntegerValue()
exists, use optional binding before calling the method.
如果動態類型obj
沒有getIntegerValue()
方法,當你初始化certainValue
的時候,系統會出現運行時錯誤。除非,你調用這個方法之前,先使用可選的綁定檢驗下obj.getIntegerValue()
是否存在
if let f = obj.getIntegerValue {
print("The value of 'obj' is \(f())")
} else {
print("'obj' does not have a 'getIntegerValue()' method")
}
// Prints "The value of 'obj' is 100"