- 構造体のイニシャライズの定義について
sample.swift
struct SimplePlus {
var plus: Int
var Message: String
// 一つ目のイニシャライザ。
init(number1: Int, number2: Int){
plus = number1 + number2
Message = ""
}
// 二つ目のイニシャライザ。
init(number1: String, number2: String){
plus = Int(number1)! + Int(number2)!
Message = "\(number1) + \(number2) = \(plus)"
}
}
var ans1 = SimplePlus(number1: 4, number2: 5)
print(ans1.plus)
var ans2 = SimplePlus(number1: "1", number2: "2")
print(ans2.Message)