Some types of conversion methods in Swift programming _swift

Source: Internet
Author: User


Validates the type ' type conversion ' of an instance in Swift language programming. It is used to check whether an instance type belongs to a particular superclass or subclass or its own hierarchy definition.



Swift type conversion provides two operators: "Is" checks the type of the value and ' as ' converts the type value to a different type value. Type conversions Also check whether the instance type conforms to a specific protocol conformance standard.



Define a class hierarchy
A type conversion is used to check the type of an instance or it belongs to a particular type. In addition, examine the class and its subclass hierarchy to examine and transform the instances to make them the same hierarchical structure.


 code as follows:



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)")





When we use playground to run the above program, we get the following results.




Instance Physics is:solid Physics Instance equation Is:hertz Instance Physics is:fluid Dynamics Instance
fo Rmulae Is:giga Hertz


Type checking
for type checking, use the ' is ' operator. The ' is ' operator checks whether a type instance belongs to a specific subtype, and returns ' true ' if it belongs to the instance.


code as follows:



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")





When we use playground to run the above program, we get the following results.




Instance Physics is:solid Physics Instance equation Is:hertz Instance Physics is:fluid Dynamics Instance
fo Rmulae Is:giga Hertz
Subjects in chemistry contains 2 topics and maths contains 3 topics


Convert down
a subtype of a downward type conversion can have two operators (such as: as?). and as!). As? The value is nil and returns an optional. It is used to check successful downward transitions.



"As!" returns the forced solution package, such as an optional chain, and the downward conversion returns the nil value. It is used to trigger run-time errors when a downward transition fails


 code as follows:



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)")
}
}





When we use playground to run the above program, we get the following results.




Instance Physics is:solid Physics Instance equation Is:hertz Instance Physics is:fluid Dynamics Instance
fo Rmulae Is:giga Hertz
chemistry topics are: ' Solid physics ', Hertz Maths topics are
: ' Fluid Dynamics ', Giga hertz< C6/>chemistry topics are: ' Thermo physics ', decibels maths topics are
: ' Astro physics ', Megahertz maths topics-are
: ' Differential equations ', cosine Series 


Type conversions: any object with any
to indicate that the instance belongs to any type including the function type, use the "any" keyword


 code as follows:



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")
}
}





When we use playground to run the above program, we get the following results.




Instance Physics is:solid Physics Instance equation Is:hertz Instance Physics is:fluid Dynamics Instance
fo Rmulae Is:giga Hertz
chemistry topics are: ' Solid physics ', Hertz Maths topics are
: ' Fluid Dynamics ', Giga hertz< C6/>chemistry topics are: ' Thermo physics ', decibels maths topics are
: ' Astro physics ', Megahertz maths topics-are
: ' Differential equations ', cosine Series
Integer value is/
Pi value is 3.14159
to any
Example S ' Solid physics ', Hertz
anyobject


To represent a class instance of any type, use the Anyobject keyword


 code as follows:



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")
}
}





When we use playground to run the above program, we get the following results.




Instance Physics is:solid Physics Instance equation Is:hertz Instance Physics is:fluid Dynamics Instance
fo Rmulae Is:giga Hertz
chemistry topics are: ' Solid physics ', Hertz Maths topics are
: ' Fluid Dynamics ', Giga hertz< C6/>chemistry topics are: ' Thermo physics ', decibels maths topics are
: ' Astro physics ', Megahertz maths topics-are
: ' Differential equations ', cosine Series
Integer value is/
Pi value is 3.14159
to any
Example S ' Solid physics ', Hertz




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.