TARGET_OS_IPHONE and ApplicationTests

前端 未结 4 1328
我寻月下人不归
我寻月下人不归 2021-01-01 12:35

Why doesn\'t this code work when compiling an ApplicationTests unit test bundle?

#if TARGET_OS_IPHONE
   #import 
   #import &         


        
相关标签:
4条回答
  • 2021-01-01 13:05

    You need to add

    #include <TargetConditionals.h>
    

    source: https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-8A428/TargetConditionals.h.auto.html

    0 讨论(0)
  • 2021-01-01 13:06

    Both work well

    • #import "TargetConditionals.h"
    • #import <Foundation/Foundation.h>
    <Foundation/Foundation.h> 
        |
        └-#import <Foundation/NSObjCRuntime.h>
            |
            └- #include <TargetConditionals.h> 
                    |
                    └- defined TARGET_OS_IPHONE
    
    0 讨论(0)
  • 2021-01-01 13:10

    The simplest solution is to move the #import <Foundation/Foundation.h> statement out if the #if condition and replace Cocoa with AppKit like this:

    #import <Foundation/Foundation.h>
    #if TARGET_OS_IPHONE
       #import <UIKit/UIKit.h>
    #else
       #import <AppKit/AppKit.h>
    #endif
    

    The Foundation umbrella header imports the NSObjCRuntime header which in turn imports the TargetConditionals header.

    0 讨论(0)
  • 2021-01-01 13:11

    I had a similar problem: TARGET_OS_IPHONE isn't defined when building a static library. My solution was to add "-DTARGET_OS_IPHONE" to the "Other C Flags" section of the target build options.

    0 讨论(0)
提交回复
热议问题