What’s the difference be...
What’s the difference be...
In Swift, Any
and AnyObject
are used to represent values of unknown types, but they serve different purposes and have distinct characteristics.
Any
Any
is the most general type in Swift and can represent any type, including value types (such as structs and enums) and reference types (such as classes and protocols).let mixedArray: [Any] = ["Hello", 42, 3.14]
if let firstValue = mixedArray[0] as? String {
print(firstValue) // "Hello"
}
if let secondValue = mixedArray[1] as? Int {
print(secondValue) // 42
}
AnyObject
AnyObject
is a protocol to which all classes implicitly conform. It can only represent reference types (instances of classes).id
or NSObject
as a generic type. It is also useful when you want to restrict a protocol to be implemented by classes only.let imageView = UIImageView(image: nil)
let viewController = UIViewController(nibName: nil, bundle: nil)
let mixedArray: [AnyObject] = [imageView, viewController]
`...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào