0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

イニシャライザの定義(サンプル)

Posted at
  • 構造体のイニシャライズの定義について
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)



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?