需要澄清 Swift 中的 AnyObject

Need clarification on AnyObject in Swift

开始之前,我已经阅读了Swift文档。我仍在努力理解 AnyObject 实际上是什么。它是 Swift 中所有 objects/classes 的基础 class,因为 NSObject 在 Objective C 中吗?

如果我创建一个 [AnyObject] 类型的数组并用 Movie class 实例填充它,这意味着AnyObjectMovie 的基础 class class 对吗?

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!) downcast 对吗?

for object in someObjects {
    let movie = object as! Movie
    println("Movie: '\(movie.name)', dir. \(movie.director)")
}

Swift 文档指出:

AnyObject can represent an instance of any class type.

所以...在 AnyObject 是基础 class 实例的意义上表示它?

我是 Swift 的新手所以请耐心等待 :)

AnyObject 是一个协议。如果您在 Playground 中键入它并命令单击它,将弹出以下内容:

/// 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 {
}