主要学习了类型转换。通过as、is、Any、AnyObject确定变量或类的类型。
类型铸造包含两个事情:
1.类型确认:is运算符用于检查实例的类型。
2.改变类型:as运算符用于将实例强制转换为其他类型
1.类型确认(向上广播)is与type(of:)
Swift非常注重代码可读性,所以需要检查某个实例是否属于某个类型;主要可以用is和type(of:)来确定。
1.1 is
例如:
主要检查Int是否是String类型;返回false是肯定的;
var isString = Int.self is String
print(isString)
//打印结果:false
class University{
var university : String
init(university : String){
self.university = university
}
}
//Discipline:University子类;
class Discipline : University{
var discipline : String
init(university:String,discipline:String){
self.discipline = discipline
super.init(university:university)
}
}
//Student:University子类;
class Student: : University{
var student : String
init(student : String,university:String){
self.student = student
super.init(university:university)
}
}
让我们为每个类创建一个对象,并将它们组合在一个数组中。我们的目标是了解他们的类型
var array = [University(university:"MIT"),Discipline(university:"TTT",discipline:"Computer Science"),Student(student:"Chloe",university:"BITS")]
print(array is [Student])//打印结果:false
print(array is [Discipline])//打印结果:false
print(array is [University])//打印结果:true
从输出结果可以看出,数组是[University]类型;因为Student和Discipline都是University的子类,所以子类都是隐式的向上转换为父类类型;Swift类型会自动确定类的父类,并为其设置为父类的类型。
1.2 type(of:)可用于确定任何变量/常量的类型。
var university = University(university: "MIT")
var student = Student(student: "Anupam",university: "BITS")
var discipline = Discipline(university: "IIT",discipline: "Computer Science")
print(type(of:university ))//打印结果:University
print(type(of: student))//打印结果:Student
print(type(of: discipline))//打印结果:Discipline
协议也是类,所以类型确认方法也适用于协议。
protocol SomeProtocol {
init(str: String)
}
class SomeClass : SomeProtocol {
required init(str: String) {
}
}
var sc = SomeClass(str: "JournalDev.com")
print(sc is SomeProtocol) //打印结果:true
2.改变类型(向下广播)
为了将父类向下转换为子类,我们使用操作符as。
as?:如果向下转换成功,则返回一个可选值,否则返回nil
as!:强制展开值,只有当你确定绝对不会失败的时候,才能使用,否则会崩溃。
下面是个典型的示例:
let compilerError = 0.0 as Int//编译错误:0.0是典型的float类型,所以不能作为Int类型
let crashes = 0.0 as! Int//编译错误:0.0是典型的float类型,所以不能作为Int类型
现在我们尝试将数组元素向下转换为子类类型:
var array = [University(university: "MIT"),
Discipline(university: "IIT",discipline: "Computer Science"),
Student(student: "Anupam",university: "BITS"),
Student(student: "Mark",university: "MIT")]
for item in array {
if let obj = item as? Student {
print("Students Detail: \(obj.student), \(obj.university)")
} else if let obj = item as? Discipline {
print("Discipline Details: \(obj.discipline), \(obj.university)")
}
}
//打印结果:
Discipline Details: Computer Science, IIT
Students Detail: Anupam, BITS
Students Detail: Mark, MIT
3.Any和AnyObject
Any可以表示任何类型的实例,包括函数类型。
AnyObject可以表示任何类类型的实例。AnyObject可以是Any类型。
在AnyObject数组中,要设置值类型,需要使用操作符将它们键入AnyObject。
下面看个示例:
class A{
var a = "Anupam"
}
func hello()
{
print("Hello")
}
let tempArray = ["Hello","World"]
var anyType : [Any] = [1, "Hey",tempArray,true, 1.25, A(), hello()]
var anyObjectType : [AnyObject] = [A(),tempArray as AnyObject, 1 as AnyObject,"Hey" as AnyObject]
print(anyType)
print(anyObjectType)
for item in anyType {
if let obj = item as? A {
print("Class A property name is: \(obj.a)")
}
}
//打印结果:
//Class A property name is: Anupam
for item in anyObjectType {
if let obj = item as? String {
print("String type: \(type(of: obj))")
}
}
//打印结果:
//String type: String