代码之家  ›  专栏  ›  技术社区  ›  Matt Baker

目标IPHONE和应用程序测试

  •  20
  • Matt Baker  · 技术社区  · 14 年前

    为什么在编译ApplicationTests单元测试包时此代码不起作用?

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

    我的一个依赖项有这个检查,在我的主应用程序包中编译得很好,但是它尝试加载 <Cocoa/Cocoa.h> 在编译我的ApplicationTests包时。这可能是因为我对Xcode缺乏理解,但是当我的测试包无法构建时,我会感到紧张。有什么建议吗?

    4 回复  |  直到 6 年前
        1
  •  15
  •   Sujay DÅ©ng Nguyễn    8 年前

    我也有类似的问题: TARGET_OS_IPHONE 在构建静态库时未定义。我的解决办法是加上 -DTARGET_OS_IPHONE “到” Other C Flags “目标生成选项的。

        2
  •  36
  •   DomQ    4 年前
        3
  •  18
  •   fjoachim    9 年前

    最简单的解决办法是移动 #import <Foundation/Foundation.h> 声明如果 #if

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

    Foundation伞形头导入NSObjCRuntime头,后者又导入TargetConditionals头。

        4
  •  0
  •   Bill    4 年前

    两者都很好

    • #import "TargetConditionals.h"
    • #import <Foundation/Foundation.h>
    <Foundation/Foundation.h> 
        |
        └-#import <Foundation/NSObjCRuntime.h>
            |
            └- #include <TargetConditionals.h> 
                    |
                    └- defined TARGET_OS_IPHONE
    
        5
  •  0
  •   scaly    3 年前

    TARGET_OS_IPHONE TARGET_OS_IPHONE=1 GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS 在内部版本设置或.xcconfig文件中。

    细节:

    carthage bootstrap 在尝试构建时会失败 iRate 1.12.2 . 我查看了迦太基构建日志,导致失败的错误是:

    error: 'TARGET_OS_IPHONE' is not defined, evaluates to 0 [-Werror,-Wundef-prefix=TARGET_OS_]
    

    对我来说,问题是iRate已经不在开发中了,我不想用iRate来覆盖一些坏的构建设置。

    然而,有一个巧妙的解决办法,我从那里的人那里学到了 Carthage XCODE_XCCONFIG_FILE=path/to/my.xcconfig 跑步前 xcodebuild . 该.xcconfig文件中的任何设置现在都将覆盖您正在使用的任何项目的设置 Xcode生成 .

    此外,您还可以通过调用脚本(而不是调用xcodebuild)动态执行此操作,例如:

    #!/usr/bin/env bash
    
    # Save this script as 'injectXcodeBuild.sh'
    # Run it in place of xcodebuild (all arguments get forwarded through)
    # The echo'd commands below will override any settings of the
    # projects that get built by xcodebuild through this script.
    
    set -euo pipefail
    
    xcconfig=$(mktemp /tmp/static.xcconfig.XXXXXX)
    trap 'rm -f "$xcconfig"' INT TERM HUP EXIT
    
    echo 'GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS=TARGET_OS_IPHONE=1' >> $xcconfig
    
    export XCODE_XCCONFIG_FILE="$xcconfig"
    
    xcodebuild "$@"
    

    或者代替 Xcode生成 carthage 如果你需要覆盖一些迦太基依赖的构建设置。它可能也适用于椰子 pod 命令(我不确定)。