Xcode

SwiftGen사용하기

motosw3600 2022. 7. 25. 19:40

SwiftGen설치

SwiftGen

 

GitHub - SwiftGen/SwiftGen: The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all S

The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs! - GitHub - SwiftGen/SwiftGen: The Swift code generator for your assets, storyboards...

github.com

설치 방식

  • homebrew로 설치
  • cocoapods으로 설치
  • ZIP파일로 설치

여러 설치방식이 존재하며 아래는 homebrew를 통해 설치

$ brew update
$ brew install swiftgen

 

SwiftGen 설정

SwiftGen설치 후 swiftgen.yml이란 파일을 생성하여 관리한다.

swiftgen.yml파일은 terminal의 프로젝트 최상단에서 swiftgen config init을 입력하여 생성가능하다.

(프로젝트 - Build Phases - Run Script에서 아래와 같이 설정하여 생성도 가능)

Configuration에 따라 별도로 target설정이 가능(ex Debug, release)

테스트 프로젝트로는 Configuration마다 별개의 설정 없이 yml설정으로 공통적으로 진행

빌드 하면 프로젝트 최상단에 swiftgen.yml파일 생성

 

SwiftGen.yml설정

이제 swiftgen.yml에서 리소스 들을 설정하고 프로젝트 내부에서 선언한 리소스들을 사용할 수 있게 되었다.

YAML, json형태로 작성 가능하며 아래 예제에서는 YAML로 작성

swiftgen config run 또는 Run Script실행을 동작하면 프로젝트 내부에 파일이 생기고 

해당 파일을 프로젝트내에 추가

XCAssets+Generated.swift파일 안에 Asset enum정의

Asset.customColor로 접근해서 사용

 

+ Color외에도 fonts, coredata, files, Localizable Strings등 다른리소스에도 적용 가능

'Xcode' 카테고리의 다른 글

XcodeGen으로 프로젝트 관리하기  (0) 2022.07.25
Xcode 단축키  (0) 2021.12.09