English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Swiftのタイプ変換は、例のタイプを判断し、例のタイプがその親クラスまたは子クラスの例であるかどうかを確認することもできます。
Swiftでは、isおよびas演算子を使用してタイプ変換を実現します。isは値のタイプを確認し、asはタイプを変換します。
タイプ変換は、クラスが特定のプロトコルを実装しているかどうかを確認するためにも使用できます。
以下に、Subjects、Chemistry、Mathsの3つのクラスが定義されています:ChemistryとMathsはSubjectsを継承しています。
以下のコードです:
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 = [ 化学(物理学:"固体物理学", 方程式:"ヘルツ"), Maths(physics: "流体力学", formulae: "ギガヘルツ") let samplechem = Chemistry(physics: "固体物理", equations: "ヘルツ") print("サンプルの物理学は: \(samplechem.physics)") print("サンプルの方程式: \(samplechem.equations)") let samplemaths = Maths(physics: "流体力学", formulae: "ギガヘルツ") print("サンプルの物理学は: \(samplemaths.physics)") print("サンプルの公式は: \(samplemaths.formulae)")
以上のプログラムの実行結果は:
例の物理学は: 固体物理 例の方程式は: ヘルツ 例の物理学は: 流体力学 例の公式は: ギガヘルツ
タイプ変換は、例のタイプが特定の例のタイプに属するかどうかを検出するために使用されます。
それをクラスとサブクラスの階層構造に使用して、特定のクラスの例のタイプを確認し、そのクラスの例をその階層構造の中の他のタイプに変換することができます。
タイプチェックは以下のように使用されます。 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 = [ 化学(物理学:"固体物理学", 方程式:"ヘルツ"), 数学(物理学:"流体力学", 式:"ギガヘルツ"), 化学(物理学:"熱物理学", 方程式:"デシベル"), Maths(physics: "天体物理学", formulae: "テラヘルツ"),}} Maths(physics: "微分方程", formulae: "余弦級数")] let samplechem = Chemistry(physics: "固体物理", equations: "ヘルツ") print("サンプルの物理学は: \(samplechem.physics)") print("サンプルの方程式: \(samplechem.equations)") let samplemaths = Maths(physics: "流体力学", formulae: "ギガヘルツ") print("サンプルの物理学は: \(samplemaths.physics)") print("サンプルの公式は: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { // もし Chemistry タイプの例があれば true を返し、そうでない場合は false を返します。 if item is Chemistry { ++chemCount else if item is Maths { ++mathsCount } } 化学科目包含 \(chemCount) 件のトピック、数学は \(mathsCount) 件のトピックを含んでいます。
以上のプログラムの実行結果は:
例の物理学は: 固体物理 例の方程式は: ヘルツ 例の物理学は: 流体力学 例の公式は: ギガヘルツ 化学の科目には含まれています 2 のトピック、数学には含まれています 3 のトピック
下層型への変換は、型変換演算子(as? または as!)を使用して行います
下層型への変換が成功するかどうかが不明な場合、条件付きの変換(as?)を使用します。条件付きの変換は常にオプショナル値(オプショナル値)を返し、下層型への変換が不可能な場合はオプショナル値は nil になります。
強制的な変換(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 = [ 化学(物理学:"固体物理学", 方程式:"ヘルツ"), 数学(物理学:"流体力学", 式:"ギガヘルツ"), 化学(物理学:"熱物理学", 方程式:"デシベル"), Maths(physics: "天体物理学", formulae: "テラヘルツ"),}} Maths(physics: "微分方程", formulae: "余弦級数")] let samplechem = Chemistry(physics: "固体物理", equations: "ヘルツ") print("サンプルの物理学は: \(samplechem.physics)") print("サンプルの方程式: \(samplechem.equations)") let samplemaths = Maths(physics: "流体力学", formulae: "ギガヘルツ") print("サンプルの物理学は: \(samplemaths.physics)") print("サンプルの公式は: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { // 型変換の条件形式 if let show = item as? Chemistry { print("化学のテーマは: '\(show.physics)', \(show.equations)") // 強制形式 } else if let example = item as? Maths { print("数学のテーマは: '\(example.physics)', \(example.formulae)") } }
以上のプログラムの実行結果は:
例の物理学は: 固体物理 例の方程式は: ヘルツ 例の物理学は: 流体力学 例の公式は: ギガヘルツ 化学のトピックは: '固体物理', ヘルツ 数学のトピックは: '流体力学', ギガヘルツ 化学のトピックは: '熱物理学', デシベル 数学のトピックは: '天体物理学', メガヘルツ 数学のトピックは: '微分方程', コーシー級数
Swift は不定型に2つの特別な型エイシェイズを提供しています:
AnyObject
どんな class 型の例も表すことができます。Any
どんな型も表すことができます、包括してメソッド型(function types)も。注意:
その行動や機能を明確に必要とする場合にのみ使用してくださいAny
および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 sa = [ 化学(物理学:"固体物理学", 方程式:"ヘルツ"), 数学(物理学:"流体力学", 式:"ギガヘルツ"), 化学(物理学:"熱物理学", 方程式:"デシベル"), Maths(physics: "天体物理学", formulae: "テラヘルツ"),}} Maths(physics: "微分方程", formulae: "余弦級数")] let samplechem = Chemistry(physics: "固体物理", equations: "ヘルツ") print("サンプルの物理学は: \(samplechem.physics)") print("サンプルの方程式: \(samplechem.equations)") let samplemaths = Maths(physics: "流体力学", formulae: "ギガヘルツ") print("サンプルの物理学は: \(samplemaths.physics)") print("サンプルの公式は: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { // 型変換の条件形式 if let show = item as? Chemistry { print("化学のテーマは: '\(show.physics)', \(show.equations)") // 強制形式 } else if let example = item as? Maths { print("数学のテーマは: '\(example.physics)', \(example.formulae)") } } // Any 型の配列 exampleany に格納できます var exampleany = [Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("Any サンプル") exampleany.append(Chemistry(physics: "固体物理", equations: "テラヘルツ")) for item2 in exampleany { switch item2 { case let someInt as Int: print("整数の値は \(someInt)") case let someDouble as Double where someDouble > 0: print("ピの値は \(someDouble)") case let someString as String: print("\(someString)") case let phy as Chemistry: print("トピック '\(phy.physics)', \(phy.equations)") default: print("None") } }
以上のプログラムの実行結果は:
例の物理学は: 固体物理 例の方程式は: ヘルツ 例の物理学は: 流体力学 例の公式は: ギガヘルツ 化学のトピックは: '固体物理', ヘルツ 数学のトピックは: '流体力学', ギガヘルツ 化学のトピックは: '熱物理学', デシベル 数学のトピックは: '天体物理学', メガヘルツ 数学のトピックは: '微分方程', コーシー級数 整型の値は 12 Piの値は 3.14159 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) } } // [AnyObject] 型の配列 let saprint: [AnyObject] = [ 化学(物理学:"固体物理学", 方程式:"ヘルツ"), 数学(物理学:"流体力学", 式:"ギガヘルツ"), 化学(物理学:"熱物理学", 方程式:"デシベル"), Maths(physics: "天体物理学", formulae: "テラヘルツ"),}} Maths(physics: "微分方程", formulae: "余弦級数")] let samplechem = Chemistry(physics: "固体物理", equations: "ヘルツ") print("サンプルの物理学は: \(samplechem.physics)") print("サンプルの方程式: \(samplechem.equations)") let samplemaths = Maths(physics: "流体力学", formulae: "ギガヘルツ") print("サンプルの物理学は: \(samplemaths.physics)") print("サンプルの公式は: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in saprint { // 型変換の条件形式 if let show = item as? Chemistry { print("化学のテーマは: '\(show.physics)', \(show.equations)") // 強制形式 } else if let example = item as? Maths { print("数学のテーマは: '\(example.physics)', \(example.formulae)") } } var exampleany = [Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("Any サンプル") exampleany.append(Chemistry(physics: "固体物理", equations: "テラヘルツ")) for item2 in exampleany { switch item2 { case let someInt as Int: print("整数の値は \(someInt)") case let someDouble as Double where someDouble > 0: print("ピの値は \(someDouble)") case let someString as String: print("\(someString)") case let phy as Chemistry: print("トピック '\(phy.physics)', \(phy.equations)") default: print("None") } }
以上のプログラムの実行結果は:
例の物理学は: 固体物理 例の方程式は: ヘルツ 例の物理学は: 流体力学 例の公式は: ギガヘルツ 化学のトピックは: '固体物理', ヘルツ 数学のトピックは: '流体力学', ギガヘルツ 化学のトピックは: '熱物理学', デシベル 数学のトピックは: '天体物理学', メガヘルツ 数学のトピックは: '微分方程', コーシー級数 整型の値は 12 Piの値は 3.14159 Any 例 トピック '固体物理', メガヘルツ
switch文のcaseで強制形式の型変換演算子(as、as?ではなく)を使用して明確な型へのチェックおよび変換を行います。