LoginSignup
1
1

More than 3 years have passed since last update.

[SwiftUI]Appプロトコル

Posted at

XcodeでiOSの新規Appプロジェクトを生成すると雛形から作られるのが、Appプロトコルを実装する〜Appクラスだ。

import SwiftUI
 
@main
struct LandmarksApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

計算型プロパティbodyの実装は必須となる。

WindowGroupはビュー階層のコンテナ。

ContentViewはアプリで独自に実装したビュー。

import SwiftUI
 
struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .padding()
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Viewプロトコルを実装したContentViewがアプリ独自のビュー。PreviewProviderプロトコルを実装したContentView_PreviewsはXcodeのプレビュー表示にContentViewを表示させるためのもの。

ContentViewの計算型プロパティbodyにビューの内容を実装する。

サンプルコードでは、文言"Hello, World!"をテキスト描画している。

【関連情報】
Creating and Combining Views

App

Cocoa.swift

Cocoa勉強会 関東

Cocoa練習帳

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