0
1

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 5 years have passed since last update.

Swift 構造体

Last updated at Posted at 2018-11-08

構造体(struct)とは

クラスと同様にカプセル化を実現する方法として提供されている機能。


struct selfIntroduction {
    var name: String
    var age: Int
}

var all = selfIntroduction(name: "kunio", age: 21)

print("name: \(all.name)")
print("age:\(all.age)")

初期値がある場合インスタンスに引数を設定する必要はない。


struct selfIntroduction {
    var name: String = "kunio" 
    var age: Int "21"
}

var all = selfIntroduction()

print("name: \(all.name)")
print("age:\(all.age)")

イニシャライザについて
https://qiita.com/KunioTerada/items/1a7611924b8e97668c6f

クラスと構造体の違い

大きな違いは2つある

  • 参照型であるか
  • 継承が可能であるか

クラスは参照型であり、継承ができる。
構造体は値型であり、継承ができない。

随時更新

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?