首页 > 其他分享 >无涯教程-Swift - 类型转换

无涯教程-Swift - 类型转换

时间:2024-01-29 19:31:35浏览次数:31  
标签:类型转换 String Swift 无涯 Hertz equations formulae print physics

为了验证的类型,"Type Casting"以Swift 4语言开始发挥作用,它用于检查类型是属于特定的超类还是子类,或者是否在其自己的层次结构中定义。

Swift 4类型转换提供两个运算符" 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")
print("Instance physics is:\(samplechem.physics)")
print("Instance equation is:\(samplechem.equations)")

let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
print("Instance physics is:\(samplemaths.physics)")
print("Instance formulae is:\(samplemaths.formulae)")

运行上述程序时,我们得到以下输出-

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz

类型检查

类型检查是使用" 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")
print("Instance physics is:\(samplechem.physics)")
print("Instance equation is:\(samplechem.equations)")

let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
print("Instance physics is:\(samplemaths.physics)")
print("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
   }
}

print("Subjects in chemistry contains\(chemCount) topics and maths contains\(mathsCount) topics")

运行上述程序时,我们得到以下输出-

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Subjects in chemistry contains 2 topics and maths contains 3 topics

向下转换

向下转换子类类型可以使用两个运算符(as和as!)完成,当值返回nil时,返回一个可选值,它用于检查成功的向下转换。

'如!'当向下转换返回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")
print("Instance physics is:\(samplechem.physics)")
print("Instance equation is:\(samplechem.equations)")

let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
print("Instance physics is:\(samplemaths.physics)")
print("Instance formulae is:\(samplemaths.formulae)")

var chemCount=0
var mathsCount=0

for item in sa {
   if let print=item as? Chemistry {
      print("Chemistry topics are: '\(print.physics)',\(print.equations)")
   } else if let example=item as? Maths {
      print("Maths topics are: '\(example.physics)',\(example.formulae)")
   }
}

运行上述程序时,我们得到以下输出-

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Chemistry topics are: 'solid physics', Hertz
Maths topics are: 'Fluid Dynamics', Giga Hertz
Chemistry topics are: 'Thermo physics', Decibels
Maths topics are: 'Astro Physics', MegaHertz
Maths topics are: 'Differential Equations', Cosine Series

类型转换

关键字" 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")
print("Instance physics is:\(samplechem.physics)")
print("Instance equation is:\(samplechem.equations)")

let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
print("Instance physics is:\(samplemaths.physics)")
print("Instance formulae is:\(samplemaths.formulae)")

var chemCount=0
var mathsCount=0

for item in sa {
   if let print=item as? Chemistry {
      print("Chemistry topics are: '\(print.physics)',\(print.equations)")
   } else if let example=item as? Maths {
      print("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:
         print("Integer value is\(someInt)")
      case let someDouble as Double where someDouble > 0:
         print("Pi value is\(someDouble)")
      case let someString as String:
         print("\(someString)")
      case let phy as Chemistry:   
         print("Topics '\(phy.physics)',\(phy.equations)")
      default:
         print("None")
   }
}

运行上述程序时,我们得到以下输出-

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Chemistry topics are: 'solid physics', Hertz
Maths topics are: 'Fluid Dynamics', Giga Hertz
Chemistry topics are: 'Thermo physics', Decibels
Maths topics are: 'Astro Physics', MegaHertz
Maths topics are: 'Differential Equations', Cosine Series
Integer value is 12
Pi value is 3.14159
Example for Any
Topics 'solid physics', Hertz

AnyObject

为了表示任何类类型的,使用了" 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")
print("Instance physics is:\(samplechem.physics)")
print("Instance equation is:\(samplechem.equations)")

let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
print("Instance physics is:\(samplemaths.physics)")
print("Instance formulae is:\(samplemaths.formulae)")

var chemCount=0
var mathsCount=0

for item in saprint {
   if let print=item as? Chemistry {
      print("Chemistry topics are: '\(print.physics)',\(print.equations)")
   } else if let example=item as? Maths {
      print("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:
         print("Integer value is\(someInt)")
      case let someDouble as Double where someDouble > 0:
         print("Pi value is\(someDouble)")
      case let someString as String:
         print("\(someString)")
      case let phy as Chemistry:
         print("Topics '\(phy.physics)',\(phy.equations)")
      default:
         print("None")
   }
}

运行上述程序时,我们得到以下输出-

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Chemistry topics are: 'solid physics', Hertz
Maths topics are: 'Fluid Dynamics', Giga Hertz
Chemistry topics are: 'Thermo physics', Decibels
Maths topics are: 'Astro Physics', MegaHertz
Maths topics are: 'Differential Equations', Cosine Series
Integer value is 12
Pi value is 3.14159
Example for Any
Topics 'solid physics', Hertz

参考链接

https://www.learnfk.com/swift/swift-type-casting.html

标签:类型转换,String,Swift,无涯,Hertz,equations,formulae,print,physics
From: https://blog.51cto.com/u_14033984/9472224

相关文章

  • 无涯教程-Swift - 引用计数
    内存管理函数及其用法通过自动引用计数(ARC)以Swift4语言处理。ARC用于初始化和取消初始化系统资源,从而在不再需要时释放类使用的内存空间。ARC跟踪有关我们的代码之间的关系的信息,以有效地管理内存资源。ARC函数每次通过init()创建新的类时,ARC每次都会分配一块内存来存储信......
  • 无涯教程-Swift - 解析构造
    在需要释放一个类实例之前,必须调用"deinitializer"来释放内存空间,关键字"deinit"用于取消分配系统资源占用的内存空间。释放内存空间当不再需要实例时,Swift4会自动释放其实例,以释放资源。Swift4通过自动引用计数(ARC)处理实例的内存管理,如自动引用计数中所述。通常,在实例......
  • 无涯教程-Swift - 继承
    通常,一个类可以从另一个类继承方法,属性和函数。类可以进一步分类为子类和父类。子类  - 当一个类从另一个类继承属性,方法和函数时,称为子类父类  - 包含从其自身继承其他类的属性,方法和函数的类称为超类Swift4类包含调用和访问方法,属性,函数和覆盖方法的父类,此外,......
  • 无涯教程-Swift - Subscripts(下标)
    Subscripts下标可以访问类,结构和枚举中的集合,序列和列表的元素成员。这些下标用于在索引的帮助下存储和检索值。在someArray[index]的帮助下访问数组元素,而在Dictionary中的后续成员元素可以作为someDicitonary[key]访问。下标声明语法 "subscript"关键字用于定义,用户可以......
  • 无涯教程-Swift - Method(方法)
    在Swift4语言中,与特定类型关联的函数称为"方法"。在ObjectiveC中,类用于定义方法,而Swift4语言为用户提供了具有类,结构和枚举方法的灵活性。实例方法在Swift4语言中,可通过方法访问Class,Structures和Enumeration。方法可以写在{}花括号内,它具有对类型的方法和属性的隐式访问,......
  • 无涯教程-Swift - Properties(属性)
    Swift4语言为类,枚举或结构提供属性以关联值。存储属性Swift4引入了存储属性的概念来存储常量和变量的,常量的存储属性由'let'关键字定义,变量的存储属性由'var'关键字定义。在定义期间,存储的属性提供"默认值"初始化期间,用户可以初始化和修改初始值structNumber{var......
  • 无涯教程-Swift - Struc(结构)
    Swift4提供了一个灵活的构建块,可以将结构struct用作结构,通过使用这些结构,可以一次定义构造方法和属性。结构语法Structuresaredefinedwitha'Struct'Keyword.structnameStruct{Definition1Definition2---DefinitionN}结构定义如假设我们必须访......
  • 无涯教程-Swift - 集合(Sets)
    Swift4Sets用于存储相同类型的不同值,但它们没有数组的确定顺序,如果要确保没有重复的值,则可以使用Set集合而不是数组。创建Set集您可以使用以下初始化语法创建一个特定类型的空集-varsomeSet=Set<Character>()//字符可以替换为set的数据类型。访问和修改您可以使用......
  • 无涯教程-Swift - 数组
    Swift4数组用于存储相同类型的值的有序列表。如果将创建的数组分配给变量,则它始终是可变的,这意味着您可以通过添加,删除或更改其元素来对其进行更改;但是,如果将数组分配给常量,则该数组是不可变的,并且其大小和内容无法更改。创建数组您可以使用以下初始化器语法创建某种类型的空......
  • 无涯教程-Swift - 循环语句
    在某些情况下,您需要多次执行一个代码块。通常,语句是按顺序执行的:函数中的第一个语句首先执行,然后第二个执行,依此类推。循环语句使我们可以多次执行一个语句或一组语句。以下是大多数编程语言中循环语句的概述-Swift4编程语言提供了以下种类的循环来处理循环需求。Sr.NoLoop......