These concepts make people very confused, read a lot of posts, finally figured out, simple summary:
AnyAnd AnyObject is the product of two compromises in Swift. What do you mean, there is an ID keyword in OC, which means any object, what is the corresponding ID when OC and swift are mixed? I invented the anyobject. But! OC in the Nsstring,nsarray and so are class, but in Swift String,array is a struct, how to do it, mixed when nsstring type corresponding ID can not be used anyobject, in order to pits, And make an any, which means any type. Look at the definition of the official document:
class an instance of type any can represent any type, even including the method (func) Type
And Anyclass is just a anyobject alias. The same as Anyobject.
Let's talk first AnyObject . Readers who have written objective-c may know that there is a magical thing called in Objective-c id . The compiler does not type-check the variables that are declared id , it can represent concepts such as instances of any class. Many places in the COCOA framework are used id to perform such tasks as parameter passing and method return, which is a representation of the dynamic characteristics of objective-c. The main use of Swift today remains the use of the COCOA framework for app development, so in order to work with the Cocoa architecture, the original id concept is used in a similar style that can represent any class type of AnyObject substitution.
But there is an essential difference between the two. In Swift, not only does the compiler not AnyObject check the instance's method calls, but even AnyObject all of the method calls will return the results of Optional. While this is in line with Objective-c's philosophy, it is very cumbersome and dangerous to use in Swift environments. The choice should be to determine the AnyObject actual type and then make the call when you use it.
Assuming that one of the original APIs is returning one id , then in Swift it will now be mapped as AnyObject? (because id it can be pointed nil , so here we need a Optional version), although we know that the call should be no problem , but we'd still better write this:
FuncSomeMethod()-Anyobject?{// ...Returns an Anyobject?, which is equivalent to returning an ID in objective-cReturnResult}let anyobject: Span class= "kt" >anyobject= someclass. () if let someinstance = anyObject as? somerealclass {//... //here we got a concrete example of Somerealclass someinstance. () }
If we notice AnyObject the definition, we can see that it is actually an interface:
protocol AnyObject {}
What's special is that all of class this implicitly implements this interface, which is why it is AnyObject only applicable to class types. And all of the basic types in Swift, including Array those in the Dictionary traditional sense, are class all struct types and cannot be AnyObject represented, so Apple presents a more special Any , except class , it can also represent struct enum all types including and.
For an in-depth understanding, give a very interesting example. For experiments Any and AnyObject features, write the following code in Playground:
ImportUIKitLetswiftint: int = 1let swiftstring: string = "Miao" var array: [anyobject ] = []array. (swiftint) array. (swiftstring)
We have declared here one Int and the String other, supposedly they should only be Any represented, not AnyObject represented. But you will find that this code can be compiled and run through. Does that mean that Apple's programming guidelines are in fact wrong? This is not the case, you can print array , you will find that the elements in fact has become a NSNumber and NSString , here has been an automatic conversion. Because of import us UIKit (in fact here we need only Foundation , and at the time of import will UIKit also be Foundation imported), in Swift and Cocoa the corresponding types can be automatically converted. Because we explicitly understand the need, the AnyObject compiler thinks we need the Cocoa type instead of the native type, and we do an automatic conversion.
In the above code, if we get import UIKit rid of it, we will get a compilation error that cannot be adapted AnyObject . What we need to do is array change the declaration [AnyObject] [Any] , and everything is right.
LetSwiftint: int = 1 let swiftstring: String = "Miao" var array: [Any< Span class= "P" >] = []array. Append (swiftint) array. (swiftstring) array
Incidentally, it is helpful to improve performance by using only the Swift type instead of the Cocoa type, so we should use the native type as much as possible.
In fact, to be honest, using Any and not being a AnyObject pleasant thing, as I said at the beginning, is a compromise. If we need a lot of frequent use of both in our own code, it often means that the code may have problems with the structure and design and should be reviewed in a timely manner. To put it simply, we'd better avoid relying on and using both, and try to explicitly indicate the type of certainty.
Reprinted from: Any and anyobject
The difference between any,anyobject,anyclass in Swift