The idea behind any and anyobject :
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-C | Swift 2 | Swift 3 |
---|---|---|
id | AnyObject | Any |
NSArray * | [AnyObject] | [Any] |
NSDictionary * | [NSObject: AnyObject] | [AnyHashable: Any] |
NSSet * | Set<NSObject> | Set<AnyHashable> |
What is Any in Swift:
Any
and AnyObject
.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
is a protocol to which all classes implicitly conform.AnyObject can even be used because of the concrete kind for an instance of a kind that bridges to an Objective-C category. several 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 (as
,as?
, oras!
).
What’s the difference between Any and AnyObject?:
When should we choose Any or AnyObject? :
Memory Management with ARC and Avoiding retain cycles with Weak and Unowned in Swift.
How to create Certificate Signing Request (CSR) in macOS (iOS) Keychain Access
Swift 5.2 Generics Explained - How to Use Generics in iOS Apps
How to generate .ipa file for In-House and distribute enterprise iOS app inside a company
How to Create an App ID and Bundle Identifier for your iOS App in Xcode 11 (2020)
Creating the iOS Development & Distribution Certificate and .p12 File
Development Xcode11 Import Development and Distribution Provisioning Profile
0 Comments