需要澄清Swift中的任何对象吗
在开始之前,我已经阅读了Swift文档。我仍在试图理解任何对象实际上是什么。它是否是Swift中所有对象/类的基类,如目标C中的NSObject 如果我创建一个[AnyObject]类型的数组,并用Movie类实例填充它,这意味着AnyObject是Movie类的基类,对吗需要澄清Swift中的任何对象吗,swift,casting,nsobject,typecast-operator,Swift,Casting,Nsobject,Typecast Operator,在开始之前,我已经阅读了Swift文档。我仍在试图理解任何对象实际上是什么。它是否是Swift中所有对象/类的基类,如目标C中的NSObject 如果我创建一个[AnyObject]类型的数组,并用Movie类实例填充它,这意味着AnyObject是Movie类的基类,对吗 let someObjects: [AnyObject] = [ Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"), Movi
let someObjects: [AnyObject] = [
Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"),
Movie(name: "Moon", director: "Duncan Jones"),
Movie(name: "Alien", director: "Ridley Scott")
]
这应该是真的,否则您将无法使用类型转换操作符(as!)进行向下转换,对吗
Swift文件规定:
任何对象都可以表示任何类类型的实例
那么…从任何对象都是基类实例的角度来表示它
我是Swift新手,请耐心点:)
任何对象都是协议。如果在操场中键入并单击命令,将弹出以下窗口:
/// The protocol to which all classes implicitly conform.
///
/// When used as a concrete type, all known `@objc` methods and
/// properties are available, as implicitly-unwrapped-optional methods
/// and properties respectively, on each instance of `AnyObject`. For
/// example:
///
/// .. parsed-literal:
///
/// class C {
/// @objc func getCValue() -> Int { return 42 }
/// }
///
/// // If x has a method @objc getValue()->Int, call it and
/// // return the result. Otherwise, return nil.
/// func getCValue1(x: AnyObject) -> Int? {
/// if let f: ()->Int = **x.getCValue** {
/// return f()
/// }
/// return nil
/// }
///
/// // A more idiomatic implementation using "optional chaining"
/// func getCValue2(x: AnyObject) -> Int? {
/// return **x.getCValue?()**
/// }
///
/// // An implementation that assumes the required method is present
/// func getCValue3(x: AnyObject) -> **Int** {
/// return **x.getCValue()** // x.getCValue is implicitly unwrapped.
/// }
///
/// See also: `AnyClass`
@objc protocol AnyObject {
}
@瓦卡瓦马已经对任何物体都是什么给出了一个很好的答案。(投票)它是所有类类型对象的协议。在swift中,协议是一种类型,因此AnyObject表示“符合此协议的任何对象”。我想提一下它不是什么。它不是基类。与Objective-C等其他OO语言不同,Swift并没有为所有对象提供公共基类。相反,所有类对象都符合AnyObject协议。@Duncac那么你是说所有类对象自动符合AnyObject协议?是的,所有类对象都符合AnyObject协议。因此,所有实例都可以分配给AnyObject变量,所有类实例都可以放置在[AnyObject]
数组中。您不能扩展AnyObject
协议,因此在这方面,它的行为不像基类。@vacawama-Ahh。非常感谢。这就解释了一切!我希望他们能在文件中说明这一点。:)
/// The protocol to which all classes implicitly conform.
///
/// When used as a concrete type, all known `@objc` methods and
/// properties are available, as implicitly-unwrapped-optional methods
/// and properties respectively, on each instance of `AnyObject`. For
/// example:
///
/// .. parsed-literal:
///
/// class C {
/// @objc func getCValue() -> Int { return 42 }
/// }
///
/// // If x has a method @objc getValue()->Int, call it and
/// // return the result. Otherwise, return nil.
/// func getCValue1(x: AnyObject) -> Int? {
/// if let f: ()->Int = **x.getCValue** {
/// return f()
/// }
/// return nil
/// }
///
/// // A more idiomatic implementation using "optional chaining"
/// func getCValue2(x: AnyObject) -> Int? {
/// return **x.getCValue?()**
/// }
///
/// // An implementation that assumes the required method is present
/// func getCValue3(x: AnyObject) -> **Int** {
/// return **x.getCValue()** // x.getCValue is implicitly unwrapped.
/// }
///
/// See also: `AnyClass`
@objc protocol AnyObject {
}