The Any type in SWIFT is a unique type that can be treated as a super type of value
type and reference
type
Any
AnyObject
However, in this shot, we will be focusing on the Any
type. Let’s imagine that the height
constant has a value of 90
and is of type Int
, like this:
import Foundationlet height:Int = 90
The Int
type is specific – we’re very clear about the fact that height
is an integer number.
Let’s check out a nonspecific type that uses Any
as a type. The following code defines a values array of type [Any]
:
import Foundationlet Person:[Any] = ["Daniel", 34, "Smith", 1994]
What exactly is going on here? At first glance, it doesn’t make sense — how can the values array have numerous kinds, such as Int
and String
? This is possible because the values array’s type is [Any]
or array-of-Any
.
To make matters more mind-boggling, individual elements in values continue to use their own unique types.
For example:
import Foundationlet Person:[Any] = ["Daniel", 34, "Smith", 1994]for details in Person{switch details {case is String:print("\(details) is a string!")case is Int:print("\(details) is an integer!")default:print("I don't know this value!")}}
To traverse over the entries in the values array in the code above, we use a for loop.
A switch block is then executed for each item using the type-checking is
keyword, this switch block matches the value type with one of three cases. Here, we’re basically inspecting the kind of each item in the array, and then writing some text according to that type.
So, why aren’t the types Any
?
After all, the type of the values array is [Any]
, so its items must have type Any
, right? Well… yes and no. Here’s how:
The array values’ type is
[Any]
because it’s not particular. Individual array components have specific kinds, such asInt
andString
, but they’re still their own types.
Any
?We should know that Swift offers a few programming tools that make dealing with types more flexible before we address that issue. Consider the following examples:
All of these “tools” help you process data more effectively, and they (usually) make your code clearer, more expressive, and easier to extend and maintain.
Any
is very handy for values with a variety of non-specific types.
import Foundationlet tweetMessage:[String: Any] = ["text": "I love writing code in swift","likes": 300,"retweets": ["@dan42", "@sean", "@fortune"]]
Take a look at how this dictionary combines values of various sorts. A string is the first value, an integer is the second, and an array of strings is the third.
But, what if you want to search the tweet dictionary for specific values?
This is how you would do it:
import Foundationlet tweetMessage:[String: Any] = ["text": "I love writing code in swift","likes": 300,"retweets": ["@dan42", "@sean", "@fortune"]]if let likes = tweetMessage["likes"] {print("This tweet message has \(likes) likes!")}
The likes
constant type in the given code is Int
.
To get the value by its key, the code employs optional binding.
We can aggregate these different tweet message values into one dictionary thanks to Any
. For the tweet message, we don’t need a custom class or type, and we don’t need to use multiple values or dictionaries, which is quite beneficial!