Swiftの構文
struct
struct Game{
let title: String
let version: Double
init(title theTitle: String, version theVersion: Double ) {
self.title = theTitle
self.version = theVersion
}
func play(){
print(self.title + " is playing.....")
}
}
let MyGame = Game(title: "MyFirstGame", version: 0.1)
MyGame.play() // MyFirstGame is playing.....
プロトコル
Javaでのインターフェイスに対応する
protocol Updatable{
func update(_ version: Double)
}
struct Game: Updatable{
~~~ 略 ~~~
func update(_ version: Double) {
print("updated")
}
}
構造体名: プロトコル名とする
SwiftUI
これを踏まえたうえでContentView.swiftから引用
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
つまりはContentView構造体をViewプロトコルを使用し定義している
Viewプロトコルではbodyプロパティを実装必要があるのでそのとおりに
some型はViewプロトコルに適合した任意の型なので、必ずしもViewなくてならないわけではない