장치에서 대조적으로 시뮬레이터에서 실행할 때 조건부 컴파일
-
21-08-2019 - |
문제
내 장치와 달리 시뮬레이터를 타겟팅 할 때 다른 코드 라인을 컴파일하는 데 사용할 수있는 컴파일러 지침이 있습니까? 같은 것 :
# IF SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
# ELSE
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
# END
편집하다
해결책
#if TARGET_IPHONE_SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#else
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#endif
다른 팁
업데이트 : (더 이상 사용되지 않은/쓸모없는) 이것은 몇 년 동안 만 작동했으며 더 이상 작동하지 않습니다. (10 년 후)
레코드의 경우 Apple이 공식 샘플 코드 중 일부에서 사용하는 또 다른 방법이 있습니다.
#if TARGET_CPU_ARM
// Only executes on an iPhone or iPod touch device
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#else
// Only executes on the Simulator
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#endif
현대적인 신속한 솔루션을 찾는 사람들을 위해 (새로운) 플랫폼 조건 targetEnvironment
여기에 결정적인 답변을 제공합니다. 예를 들어:
#if targetEnvironment(simulator)
self.imagePicker.sourceType = .photoLibrary
#else
self.imagePicker.sourceType = .camera
#endif
그만큼 대상 환경 플랫폼 조건 기능이 소개되었습니다 SE-0190 그리고 그 이후로 사용할 수 있습니다 스위프트 4.1.
제휴하지 않습니다 StackOverflow