Swift의 #ifdef DEBUG 방식

2225 단어 Swift
지금까지 Objective-C를 개발할 때 디버깅할 때만 실행된 코드는 다음과 같다.
#ifdef DEBUG
    // ...
#endif
하지만 스wift에서는 같은 기술을 할 수 없다.
Apple 문서에 따라 다음과 같이 바꿀 수 있습니다.
#if build_configuration
    // ...
#elseif build_configuration
    // ...
#else
    // ...
#endif
써 보았다#if DEBUG ...,하지만 기대했던 동작은 하지 않았다.
다음과 같이 Apple 문서를 자세히 읽으십시오.
Swift code and Objective-C code are conditionally compiled in different ways. Swift code can be conditionally compiled based on the evaluation of build configurations. Build configurations include the literal true and false values, command line flags, and the platform-testing functions listed in the table below. You can specify command line flags using -D <#flag#>.
컴파일할 때의 옵션인 것 같습니다. -D flagX코드의 Build Settings를 보세요. Swift Compuiler-Custom Flags에는 Other Swift Flags라는 프로젝트가 있습니다.
여기에 -D DEBUG의 정의를 추가하여 방금 코드가 순조롭게 운행되었다.
(Xcode6은 여전히 일반적이어서 캡처를 붙일 수 없어서 유감입니다.)
[추기 2014.09.2] Xcode6가 정식으로 출시되었고 캡처가 붙여졌습니다.

새 프로젝트를 만든 후 Processor Macros는 이전과 같이 정의했지만, Other Swift Flags는 비어 있습니다.
Xcode6가 발매되었을 때 처음부터 정의가 됐으면 좋겠다DEBUG=1.
[추기 2014.09.21] 엑스코드6가 공식 발표를 해서 확인했지만 아쉽게도 베타는 GM 때와 마찬가지로 비어 있었다.서운하다

참고 자료

  • Using Swift with Cocoa with Objective-C - Interacting with C APIs
  • ios - #ifdef replacement in swift language - Stack Overflow
  • 좋은 웹페이지 즐겨찾기