我怎样才能从我的应用程序从Xcode的环境variables

我已经从robbie hanson安装了XcodeColors的Xcode插件。 (请参阅https://github.com/robbiehanson/XcodeColors )

如果我在操场上testing它

let dict = NSProcessInfo.processInfo().environment let env = dict["XcodeColors"] as? String 

env将是“是”。

但是,如果我在我的应用程序中使用相同的代码, env将是零,因为应用程序正在自己的进程上运行。

因为只有在插件安装的情况下,我才会用特定的esc序列打印彩色文本,所以我想要获取关于Xcode env var的信息。

我怎样才能做到这一点?

编辑你的scheme – >select“运行”部分 – >select“参数”选项卡 – >添加环境variables。

要小心,如果您运行不带XCode的应用程序,则不会设置环境variables。

我遇到了与XcodeColors相同的问题。 我最终通过一个简单的脚本构build阶段来解决这个问题。 它检查是否安装了XcodeColors,并在构build中设置/添加Info.plist的一个键。 因此,创build一个新的“运行脚本构build阶段”,并把它放在那里:

 xcodeColorsDir="$USER_LIBRARY_DIR/Application Support/Developer/Shared/Xcode/Plugins/XcodeColors.xcplugin/" xcodeColorsInstalled=0 if [ -d "$xcodeColorsDir" ]; then # Directory exists, therefore, XcodeColors is installed xcodeColorsInstalled=1 fi infoPlistPath="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}" existingValue=$(/usr/libexec/PlistBuddy -c "Print :XcodeColorsInstalled" "$infoPlistPath") if [ -z "$existingValue" ]; then # Key already exists so overwrite it /usr/libexec/PlistBuddy -c "Add :XcodeColorsInstalled bool $xcodeColorsInstalled" "$infoPlistPath" else # Key doesn't exist yet /usr/libexec/PlistBuddy -c "Set :XcodeColorsInstalled $xcodeColorsInstalled" "$infoPlistPath" fi 

然后,您可以在运行时使用类似于以下内容的方式访问Info.plist参数:

 func isColorizedLoggingEnabled() -> Bool { if let colorizedLoggingEnabled = NSBundle.mainBundle().infoDictionary?["XcodeColorsInstalled"] as? Bool { return colorizedLoggingEnabled } else { return false } }