[SwiftUI] Xcode 13.1에서 ios13 타겟으로 설정하기

Kang Hee Young·2022년 1월 13일
0

SwiftUI

목록 보기
4/5

Xcode는 정말 좋다.

Xcode 13.1 && ios 15.0

현재 나와있는 최신버전의 Xcode와 ios버전이다. ios13부터 SwiftUI를 지원하기 시작하였고 현재 최신버전에서 2를 뺀 ios13을 타겟으로 많은 프로젝트가 진행된다.
이번 글은 Xcode 13.1에서 신규 프로젝트를 생성하면서 타겟을 ios15에서 ios13으로 변경하여 오류가 발생하였고 그 오류를 해결한 과정을 기록한다.

project init setting
프로젝트를 처음 설정하는 화면으로 Interface가 SwiftUI or Storyboard, Language는 SwiftUI에서는 Swift만 선택 가능하고 Storyboard에서는 Objective-C가 추가로 선택가능하다.

  SwiftUI 인터페이스로 생성할 경우 나오는 파일 구조
SwiftUI 인터페이스로 생성할 경우 나오는 파일 구조이다. 우리가 ios13부터 사용하던 SceneDelegate, Appdelegate가 안보인다. ios15부터 ProjectApp이 생기면서 사라졌다.
여기서 ios 타겟을 15에서 13으로 바꾸면 수 많은 오류가 발생하고 그 오류들의 해결은 쉽지 않다.
Storyboard 인터페이스로 생성할 경우 나오는 파일 구조
기존 프로젝트와 같은 폴더 구조가 더 익숙하므로 interface를 Storyboard로 생성한 파일 구조다. OBJ-C는 고려하지 않았다.
Delegate파일들이 보인다. Storyboard를 학습하지 않은 입장에서 어떻게 앱 첫번째 화면을 지정해야하는지 조금 난감한 부분이 있다.

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
      // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
      // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
      let contentView = ContentView()
      // Use a UIHostingController as window root view controller.
      if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
//            let snapCaroucel = UIStateModel()
          window.rootViewController = UIHostingController(rootView: contentView)
          self.window = window
          window.makeKeyAndVisible()
      }
  }

SceneDelegate의 첫번째 함수를 수정하여 진입지점을 설정할 수 있다. 위 코드에서는 ContentView가 진입지점이다.

버전 하나하나 바뀔때마다 뭐가 바뀔지 설레기도 하지만 걱정되기도 하는 Xcode 최고!

profile
hekang in 42Seoul.

0개의 댓글