时间:2021-05-02
验证一个实例的类型'类型转换'在 Swift 语言编程中。它是用来检查实例类型是否属于特定超类或子类或其自己的层次结构定义。
Swift 类型转换提供两个操作符:“is” 检查值的类型和 'as' 将类型值转换为不同的类型值。 类型转换还检查实例类型是否符合特定的协议一致性标准。
定义一个类层次结构
类型转换用于检查实例的类型或者它属于特定类型。此外,检查类和它的子类层次结构来检查并转换这些实例,使之作为一个相同的层次结构。
class Subjects {
var physics: String
init(physics: String) {
self.physics = physics
}
}
class Chemistry: Subjects {
var equations: String
init(physics: String, equations: String) {
self.equations = equations
super.init(physics: physics)
}
}
class Maths: Subjects {
var formulae: String
init(physics: String, formulae: String) {
self.formulae = formulae
super.init(physics: physics)
}
}
let sa = [
Chemistry(physics: "solid physics", equations: "Hertz"),
Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")]
let samplechem = Chemistry(physics: "solid physics", equations: "Hertz")
println("Instance physics is: \(samplechem.physics)")
println("Instance equation is: \(samplechem.equations)")
let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: \(samplemaths.physics)")
println("Instance formulae is: \(samplemaths.formulae)")
当我们使用 playground 运行上面的程序,得到以下结果。
类型检查
进行类型检查,用 'is' 操作符。在 'is' 操作符检查类型实例是否属于特定的子类型,如果它属于该实例返回“true”,否则将返回“false”。
class Subjects {
var physics: String
init(physics: String) {
self.physics = physics
}
}
class Chemistry: Subjects {
var equations: String
init(physics: String, equations: String) {
self.equations = equations
super.init(physics: physics)
}
}
class Maths: Subjects {
var formulae: String
init(physics: String, formulae: String) {
self.formulae = formulae
super.init(physics: physics)
}
}
let sa = [
Chemistry(physics: "solid physics", equations: "Hertz"),
Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"),
Chemistry(physics: "Thermo physics", equations: "Decibels"),
Maths(physics: "Astro Physics", formulae: "MegaHertz"),
Maths(physics: "Differential Equations", formulae: "Cosine Series")]
let samplechem = Chemistry(physics: "solid physics", equations: "Hertz")
println("Instance physics is: \(samplechem.physics)")
println("Instance equation is: \(samplechem.equations)")
let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: \(samplemaths.physics)")
println("Instance formulae is: \(samplemaths.formulae)")
var chemCount = 0
var mathsCount = 0
for item in sa {
if item is Chemistry {
++chemCount
} else if item is Maths {
++mathsCount
}
}
println("Subjects in chemistry contains \(chemCount) topics and maths contains \(mathsCount) topics")
当我们使用 playground 运行上面的程序,得到以下结果。
向下转换
向下类型转换的子类型可以有两个操作符(如:as? 和 as!)。as? 当值是nil,返回一个可选值。它是用来检查成功向下转型。
“as!” 返回强制解包裹,如可选链,向下转换返回 nil 值。它用来触发运行时错误在向下转型出现故障时
复制代码 代码如下:
class Subjects {
var physics: String
init(physics: String) {
self.physics = physics
}
}
class Chemistry: Subjects {
var equations: String
init(physics: String, equations: String) {
self.equations = equations
super.init(physics: physics)
}
}
class Maths: Subjects {
var formulae: String
init(physics: String, formulae: String) {
self.formulae = formulae
super.init(physics: physics)
}
}
let sa = [
Chemistry(physics: "solid physics", equations: "Hertz"),
Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"),
Chemistry(physics: "Thermo physics", equations: "Decibels"),
Maths(physics: "Astro Physics", formulae: "MegaHertz"),
Maths(physics: "Differential Equations", formulae: "Cosine Series")]
let samplechem = Chemistry(physics: "solid physics", equations: "Hertz")
println("Instance physics is: \(samplechem.physics)")
println("Instance equation is: \(samplechem.equations)")
let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: \(samplemaths.physics)")
println("Instance formulae is: \(samplemaths.formulae)")
var chemCount = 0
var mathsCount = 0
for item in sa {
if let print = item as? Chemistry {
println("Chemistry topics are: '\(print.physics)', \(print.equations)")
} else if let example = item as? Maths {
println("Maths topics are: '\(example.physics)', \(example.formulae)")
}
}
当我们使用 playground 运行上面的程序,得到以下结果。
类型转换:任何与任何对象
为了表示实例属于任何类型包括函数类型,使用“Any”关键字
class Subjects {
var physics: String
init(physics: String) {
self.physics = physics
}
}
class Chemistry: Subjects {
var equations: String
init(physics: String, equations: String) {
self.equations = equations
super.init(physics: physics)
}
}
class Maths: Subjects {
var formulae: String
init(physics: String, formulae: String) {
self.formulae = formulae
super.init(physics: physics)
}
}
let sa = [
Chemistry(physics: "solid physics", equations: "Hertz"),
Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"),
Chemistry(physics: "Thermo physics", equations: "Decibels"),
Maths(physics: "Astro Physics", formulae: "MegaHertz"),
Maths(physics: "Differential Equations", formulae: "Cosine Series")]
let samplechem = Chemistry(physics: "solid physics", equations: "Hertz")
println("Instance physics is: \(samplechem.physics)")
println("Instance equation is: \(samplechem.equations)")
let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: \(samplemaths.physics)")
println("Instance formulae is: \(samplemaths.formulae)")
var chemCount = 0
var mathsCount = 0
for item in sa {
if let print = item as? Chemistry {
println("Chemistry topics are: '\(print.physics)', \(print.equations)")
} else if let example = item as? Maths {
println("Maths topics are: '\(example.physics)', \(example.formulae)")
}
}
var exampleany = [Any]()
exampleany.append(12)
exampleany.append(3.14159)
exampleany.append("Example for Any")
exampleany.append(Chemistry(physics: "solid physics", equations: "Hertz"))
for print in exampleany {
switch print {
case let someInt as Int:
println("Integer value is \(someInt)")
case let someDouble as Double where someDouble > 0:
println("Pi value is \(someDouble)")
case let someString as String:
println("\(someString)")
case let phy as Chemistry:
println("Topics '\(phy.physics)', \(phy.equations)")
default:
println("None")
}
}
当我们使用 playground 运行上面的程序,得到以下结果。
为了表示是任何类型的类实例,使用AnyObject“关键字
复制代码 代码如下:
class Subjects {
var physics: String
init(physics: String) {
self.physics = physics
}
}
class Chemistry: Subjects {
var equations: String
init(physics: String, equations: String) {
self.equations = equations
super.init(physics: physics)
}
}
class Maths: Subjects {
var formulae: String
init(physics: String, formulae: String) {
self.formulae = formulae
super.init(physics: physics)
}
}
let saprint: [AnyObject] = [Chemistry(physics: "solid physics", equations: "Hertz"),
Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"),
Chemistry(physics: "Thermo physics", equations: "Decibels"),
Maths(physics: "Astro Physics", formulae: "MegaHertz"),
Maths(physics: "Differential Equations", formulae: "Cosine Series")]
let samplechem = Chemistry(physics: "solid physics", equations: "Hertz")
println("Instance physics is: \(samplechem.physics)")
println("Instance equation is: \(samplechem.equations)")
let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: \(samplemaths.physics)")
println("Instance formulae is: \(samplemaths.formulae)")
var chemCount = 0
var mathsCount = 0
for item in saprint {
if let print = item as? Chemistry {
println("Chemistry topics are: '\(print.physics)', \(print.equations)")
} else if let example = item as? Maths {
println("Maths topics are: '\(example.physics)', \(example.formulae)")
}
}
var exampleany = [Any]()
exampleany.append(12)
exampleany.append(3.14159)
exampleany.append("Example for Any")
exampleany.append(Chemistry(physics: "solid physics", equations: "Hertz"))
for print in exampleany {
switch print {
case let someInt as Int:
println("Integer value is \(someInt)")
case let someDouble as Double where someDouble > 0:
println("Pi value is \(someDouble)")
case let someString as String:
println("\(someString)")
case let phy as Chemistry:
println("Topics '\(phy.physics)', \(phy.equations)")
default:
println("None")
}
}
当我们使用 playground 运行上面的程序,得到以下结果。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
详解Swift中对C语言接口缓存的使用以及数组与字符串转为指针类型的方法由于Swift编程语言属于上层编程语言,而Swift中由于为了低层的高性能计算接口,所以
一、引言协议约定了一些属性与方法,其作用类似Java中的抽象类,Swift中类型通过遵守协议来实现一些约定的属性和方法。Swift中的协议使用protocol关
Swift是苹果公司最新推出的编程语言,据很多人说,是用来”代替“Objective-C。但是没有确切的证据。我花了一些时间对Swift
基础类型虽然Swift是一个为开发iOS和OSXapp设计的全新编程语言,但是Swift的很多特性还是跟C和Objective-C相似。Swift也提供了与C和
PHP中类型转换,常量,系统常量,魔术常量的详解1.自动类型转换;在运算和判断时,会进行自动类型转换;1)其他类型转为bool,判断时转换;1)整型转布尔型:0