What is the difference between any and anyobject?

Time:2022-4-4

Any: it can represent any type of class, structure and enum, including functions and optional types. Basically, it can be said to be anything.

Anyobject: it refers to any instance of a category. This is only useful when using a reference type. This is equivalent to the ‘ID’ in Objective-C.

Any

If your dictionary can only be used in swift, you should use any, because your types (int, double, float, string, array, and Dictionary) are not objects.

If you will pass the dictionary to the Objective-C code that needs nsdictionary, you should use anyobject.

What is the difference between any and anyobject?

image.png

AnyObject

What is the difference between any and anyobject?

image.png

You use anyobject when you need the flexibility of an untyped object, or when you use bridged Objective-C methods and properties that return untyped results. Anyobject can be used as a concrete type for an instance of any class, class type, or class only contract.

What is the difference between any and anyobject?

image.png

The flexibility of anyobject protocol is similar to the ID type of Objective-C. Therefore, imported Objective-C types often use anyobject as the type of properties, method parameters, and return values.

Use anyobject when you handle reference types and any when you handle value types.