Xcode で最速でアプリを作る方法 🚀

https://youtu.be/ntAKQXss4Yg?si=cRbSUlyI4ZRThh43

Xcode で 最短時間 でアプリを作るなら、以下の流れで進めるのがベスト!


1️⃣ Xcode で新規プロジェクトを作成

  1. Xcode を開く → Create a new Xcode project を選択
  2. iOSApp を選択
  3. SwiftUI を選択(UIKit より速い)
  4. プロジェクト名を入力(例: FastApp
  5. Use Core Data のチェックを外す(使わないので時間短縮)
  6. Next保存先を選択Create

2️⃣ Xcode にコードをコピペ(1分)

ContentView.swift に以下をコピペするだけで、シンプルなボタンアプリが動く。

swiftコードをコピーする

import SwiftUI

struct ContentView: View {
    @State private var count = 0

    var body: some View {
        VStack {
            Text("カウント: \(count)")
                .font(.largeTitle)
                .padding()
            
            Button("タップして増やす") {
                count += 1
            }
            .font(.title2)
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(10)
        }
    }
}

#Preview {
    ContentView()
}

Xcode 14.2 で正しいプレビュー記法

あなたの環境では、以下のように書く必要があります。

swiftコードをコピーする

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

📌 SceneDelegate.swift を修正

UIKit の SceneDelegate.swift を SwiftUI 互換にする。

  1. SceneDelegate.swift を開く
  2. window.rootViewController の代わりに以下のコードを書く:
swiftコードをコピーする
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let contentView = ContentView()
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
}

3️⃣ シミュレーターで即実行(10秒)

  1. 画面上部の「▶(Run)」ボタンをクリック
  2. シミュレーターが起動し、アプリが動く! 🎉

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です