What’s the difference between Any and AnyObject ?
What’s the difference between Any and AnyObject ?
In Swift, Any and AnyObject are used to represent values of unknown types, but they serve different purposes and have distinct characteristics.
AnyAny 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]
To access the values, you need to cast them back to their original types:if let firstValue = mixedArray[0] as? String {
print(firstValue) // "Hello"
}
if let secondValue = mixedArray[1] as? Int {
print(secondValue) // 42
}
AnyObjectAnyObject is a protocol to which all classes implicitly conform. It can only represent reference types (instances of classes).Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.