When should we choose Any or AnyObject with Xcode 11, Swift 5.2 [2020]

Today, we will understand any and anyobject. Any and AnyObject. Not the same.






    The idea behind any and anyobject :

    In Objective C we don't have the concept of value types. Everything is a reference type. Even today, we have some pods written in objective c and we can easily use them in the swift project. This is known as interoperability, which is done through a concept of bridging with the help of bridging an objective C type like NSString is converted to a swift String type, this is just one example of what bridging does for us and because of this you can use pods that are written in objective c, but you can easily integrate into in your swift project but this bridging process had an unexpected behavior so in this example, NSString is a class in Objective C and classes are reference types and the string in swift is a structure and the structure is the value type so we are trying to convert a reference type into a value type.



    This caused issues in the swift 2.0 version because what happened internally in this conversion, Objective c reference types used id type to convert to anyobject in swift 2.0, and anyobject is made for reference types i.e classes you see the problem here is sometimes this conversion was fine. But sometimes this caused unexpected behavior while converting a reference type to a value type and we all know how important are value types in swift for smooth conversion there was a proposal in swift-evolution proposal number is 116 this proposal explains the problem and solution in detail.


    In swift 3 there was an implementation which maps the objective C id type it will be mapped to any and not anyobject and this was done to resolve the unexpected behavior so any and anyobject improve the bridging between objective c and swift anyobject is used for class type and any as the name suggests it can literally be anything.

    In summary, the following type mappings change from Swift 2 to Swift 3:

    Objective-CSwift 2Swift 3
    idAnyObjectAny 

    NSArray *[AnyObject][Any]

    NSDictionary *[NSObject: AnyObject][AnyHashable: Any]

    NSSet *Set<NSObject>Set<AnyHashable>


    What is Any in Swift:


    Swift provides two special types for working with nonspecific types called Any and AnyObject.
    Any can represent an instance of any (class, struct, enum) type at all, including function types. 
    AnyObject can represent an instance of any class type. It is very useful when you only work with reference types. It is equivalent to id, in Objective-C.

    Let's start an Any demo of any any anyObject.


    I will just run the application here. You can see that all the values ​​of the variables are printed in the console, so what I want to do right now is that I will change all these types with any so that I can go ahead and do it here. There is no difference here so we can see that any type it works fine with literally anything in swift be it value type or reference type.

    I am not very sure if you have observed this when I type any the Xcode intelligence does not tell me if any is it a class?, is it a structure?, is it a protocol? or is it a type alias? it's actually blank over here so let me.




    What Is AnyObject in Swift:

    Let’s go with AnyObject:-

    AnyObject can represent an instance of any class type. It is very useful when you only work with reference types. It is equivalent to id, in Objective-C.AnyObject is a protocol to which all classes implicitly conform.


    You use AnyObject when you would like the flexibleness of An untyped object or when you use bridged Objective-C ways And properties that come back an untyped result. AnyObject is used because of the concrete sort for AN instance of any category, class type, or class-only protocol. 

    The AnyObject protocol is additionally helpful to bridge the gap between Swift and Objective-C. Some Objective-C genus Apis use the AnyObject protocol to produce compatibility with Swift.
     For example anyobject demo-


    AnyObject can even be used because of the concrete kind for an instance of a kind that bridges to an Objective-C categoryseveral price sorts in Swift bridge to Objective-C counterparts, like String and Int.


    The flexible behavior of the AnyObject protocol is comparable to Objective-C's id type. For this reason, imported Objective-C types frequently use AnyObject as the type for properties, method parameters, and return values.

    Now let's talk about the issues with anyobject here I will declare a class named student instead of writing student I will declare it of the type anyobject so over here we are getting an error "value of type Anyobject has no member str "



    Casting AnyObject Instances to a Known Type:

    Anyobject only checks if the reference assigned to it is a class or not, so in order to access student name what we have to do we have to typecast obj class to student, so this is definitely some extra work that we have to do to access a simple property which I am sure would be very easy if objclass would be declared as student and not anyobject so these are some of the behavior of any and anyobject which i think you should be aware of so that you can take mature decision when you plan on using them otherwise you may end up having unwanted surprises in your code so now the question must be when should we use any and anyobject I think any has a very good use case with dictionaries in the dictionary the key can be of string type but the value can literally be anything so I would use any with dictionaries because I can store multiple types in the dictionary with any I have used any just to declare class protocol.


    Objects with a concrete type of AnyObject maintain a specific dynamic type and can be cast to that type using one of the type-cast operators (asas?, or as!).

    What’s the difference between Any and AnyObject?:

    Any and AnyObject are special types in swift, both are wont to refer types generally if we don't know the sort at the time of compilation. Any is employed to refer generally all the Swift specific data types. AnyObject is used to refer any classes, which are usually inherited by the basic class NSObject( which is the base class for all the Foundation objects), we can use this to tell the compiler only class objects are going to be considered. AnyObject is additionally used once you want to limit a protocol in order that it is often used only with classes. Any refers to any instance of a category, struct, or enum – literally anything in the least.

    When should we choose Any or AnyObject? :


    It’s a good practice to use AnyObject while you are working with references and use Any when working with value types. 

    Note: If possible, we should avoid both Any and AnyObject. It’s better to be specific.
    I am of the opinion that you should use avoiding the use of any and anyobject as much as you can and be as specific as you can in your code I really hope you like this tutorial and share it.

    Similar solutions you may also like...



    Post a Comment

    0 Comments