参考にした書籍
Swiftの文法
import UIKit
// プロトコルとは、あるクラスや構造体が実装すべきメソッドやプロパティを定義したもの
// 使用例
// protocol プロトコル名 {
// プロパティやメソッドの定義
// }
// プロトコルの宣言
protocol Protocol {
var value: Int { get }
func printValue()
}
// プロトコルの実装
struct Struct: Protocol {
var value: Int = 0
func printValue() {
print(value)
}
}
// プロトコルの実装
class Class: Protocol {
var value: Int = 0
func printValue() {
print(value)
}
}
// プロトコルを実行する
let struct1 = Struct()
struct1.printValue()
Javaの文法
public class Main {
enum Gender{
MALE,
FEMALE
}
public static void main(String[] args) throws Exception {
Gender gender_type = Gender.FEMALE;
switch(gender_type){
case MALE:
System.out.println("男性です");
break;
case FEMALE:
System.out.println("女性です");
break;
}
}
}
実行結果
女性です
Swiftでのenumも書いてみた...
enum Gender: String {
case MALE = "男性です"
case FEMALE = "女性です"
}
let gender: Gender
gender = Gender.MALE
switch gender {
case .MALE:
print(Gender.MALE.rawValue)
case .FEMALE:
print(Gender.FEMALE.rawValue)
}
playgrandで実行結果
"男性です\n"