2014-12-03 58 views
10

我已經從robbie hanson安裝了XcodeColors的Xcode插件。 (見https://github.com/robbiehanson/XcodeColors如何從我的應用程序中的Xcode獲取環境變量

如果我測試它在操場

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

ENV將是 「YES」。

但是,如果我在我的應用程序中使用相同的代碼,則env將爲零,因爲該應用程序正在其自己的進程上運行。

因爲只有在插件已安裝的情況下,我纔會打印出具有特定esc序列的彩色文本,因此我需要獲取有關Xcode env var的信息。

我該怎麼做?

+0

我具有:

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 PARAM同樣的問題。你發現了一個解決方案? – Tiago 2015-03-02 22:39:41

回答

12

編輯你的方案 - >選擇「運行」部分 - >選擇「參數」選項卡 - >添加環境變量。

要小心,如果您不使用XCode運行應用程序,則不會設置環境變量。

+0

謝謝,我也用這個方法。但是,如果我將我的源代碼分享給其他開發人員,則必須解釋如何安裝XcodeColors或不安裝XcodeColors。我想從我的應用程序自動執行。 – MUECKE446 2014-12-03 13:51:39

+0

請注意,Xcode不是由BASH運行的,所以這裏定義的環境只能存在於Xcode中。 – Efren 2017-06-07 06:01:05

+0

另請參閱有關可用環境設置的NSHipster的有用博客:http://nshipster.com/launch-arguments-and-environment-variables/ – Efren 2017-06-07 06:02:03

2

我遇到了與XcodeColors相同的問題。我最終通過一個簡單的腳本構建階段來解決它。它會檢查XcodeColors是否已安裝,並在構建中設置/添加Info.plist的密鑰。因此,創建一個新的「運行腳本生成階段」,並把這個在那裏:

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