LoginSignup
1
0

More than 3 years have passed since last update.

Swiftから始めてSwiftUI

Posted at

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なくてならないわけではない

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