/*
type casting
*/
class Car{
var name:String
init(name:String){
self.name=name
}
}
class SportCar:Car{
var speed:Int
init(speed:Int,name:String){
self.speed=speed //init self first then init super
super.init(name:name)
}
}
class BusinessCar:Car{
var seats:Int
init(name:String,seats:Int) {
self.seats=seats
super.init(name: name)
}
}
var number1=0
var number2=0
var garage=[
SportCar(speed:100,name:"xx"),
BusinessCar(name:"xx",seats:6),
SportCar(speed:200,name:"xx"),
BusinessCar(name:"xx",seats:9),
SportCar(speed:300,name:"xx"),
BusinessCar(name:"xx",seats:16),
]
for i in garage{
if i is SportCar{
print("it's a sport car")
number1 += 1
}else if i is BusinessCar{
print("it's a business car")
number2 += 1
}
}
for i in garage{
if let a=i as? SportCar{
print("it's a sp")
} else if let a=i as? BusinessCar{
print("it's a b")
}
}
/*
Any represents any type including function type
AnyObject represents any Class type
*/
var a=[Any]()
a.append(12)
a.append(12.0)
a.append("asd")
a.append((2,"daf"))
a.append(["d","df"])
a.append({(a:Int,b:Int)->Int in return a+b})
for i in a{
switch i {
case 12 as Int:
print("12 is a int here")
case 12 as Double:
print("12 is a double here")
case let someFunc as (AnyObject,AnyObject)->Int:
print("function with AnyObject is identified")
case let someFunc as (Int,Int)->Int:
print("function with Any is identified")
default:
print("nothing")
}
}