Type casting in swift : difference between is, as, as?, as!

What is the difference between is, as, as?, as! in swift? Well, lets check.

Apple doc says: Type casting is a way to check the type of an instance, or to treat that instance as a different superclass or subclass from somewhere else in its own class hierarchy.

Checking Type

Use the type check operator (is) to check whether an instance is of a certain subclass type. The type check operator returns true if the instance is of that subclass type and false if it is not.

let livingBeingObj = livingBeingArray[0] // returns a LivingBeing object.
for item in livingBeingArray {if item is Animal {print("item is of type Animal")// will get executed for first item} else if item is Human {print("item is of type Human")// will get executed for second item}}

Downcasting

Difference between as? and as!

Downcasting can be done in two ways:

  • Forced downcasting (as!).
let animalObj = livingBeingArray[0] as! Animal //forced downcasting to Animallet humanObj = livingBeingArray[1] as! Human //forced downcasting to Human
let animalObj = livingBeingArray[0] as! Human //error and crasheslet humanObj = livingBeingArray[1] as! Animal //error and crashes
let animalObj = livingBeingArray[0] as? Human //nil..animalObj is of Human? (optional Human which is the type which we tried to downcast to)let humanObj = livingBeingArray[1] as? Animal //nil..humanObj is of Animal? (optional Animal which is the type which we tried to downcast to)
let animalObj = livingBeingArray[0] as? Animal // success, returns Animal?let humanObj = livingBeingArray[1] as? Human // success, returns Human?

Upcasting

Upcasting from the base class object to its superclass is also possible. Let’s convert the animalObject created by forced downcasting back to the LivingBeing class.

let animalObj = livingBeingArray[0] as! Animal
let animalObjectAsLivingBeingObj = animalObj as LivingBeing

Type Casting for Any and AnyObject

Swift provides two special types for working with nonspecific types:

  • AnyObject can represent an instance of any class type.
Image for post
Image for post
Source: Internet
var groups = [Any]()
groups.append(1.0)
groups.append(1)
groups.append("string")

for item in groups {
switch item {
case let anInt as Int:
print("\(item) is an int")
case let aDouble as Double:
print("\(item) is a double")
case let aString as String:
print("\(item) is a string")

default:
print("dunno")
}
}

/*
1.0 is a double
1 is an int
string is a string
C11lldb_expr_13Pop (has 1 child) is a Genre
*/

Using switch statement for type checking and downcasting :

for obj in livingBeingArray
{
switch obj {
case let animalObj as Animal:
print(“\(obj) is an animal”)
break
case let humanObj as Human:
print(“\(obj) is an human”)
break
default:
print(“unknown type”)
}
}
for obj in livingBeingArray
{
switch obj {
case is Animal:
print(“\(obj) is an animal”)
break
case is Human:
print(“\(obj) is an human”)
break
default:
print(“unknown type”)
}
}

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store