2017-02-21 26 views
0

我有一個是包含在具有以下名稱捆綁文件:使用編譯器指令發出警告,如果文件丟失

databaseX.sqlite 

其中X是應用程序的版本。如果版本爲2.8,則該文件應該命名爲database2.8.sqlite。當應用程序提交給Apple時,我必須確保包含此文件。

是否有可能創建一個編譯器指令來檢查文件是否在包中?

我已經試過了,沒有成功

#define fileInBundle [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"LoteriaMac%@.sqlite", [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"]]] 

#if defined(fileInBundle) 
#pragma message("file in bundle") 
#else 
#pragma message("file missing") 
#endif 

file in bundle總是顯示,即使該文件是不捆綁。

回答

0

這是不可能的。您正試圖在編譯指令中使用運行時檢查。

通常情況下,編譯時無法知道包中是否有文件,因爲這些文件通常在代碼上獨立添加到包中,編譯後。

這與編譯時檢查另一臺計算機上的文件系統中是否存在文件相同。

要檢查在編譯時間,您可以創建自定義構建腳本(構建階段=>+按鈕)在你的目標,類似於:

APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}" 

// there is probably some easier way to get the version than from the Info.plist 
INFO_FILE="${APP_PATH}/Info.plist" 
VERSION=`/usr/libexec/plistbuddy -c Print:CFBundleShortVersionString "${INFO_FILE}"` 

// the file we want to exist 
DB_FILE="${APP_PATH}/database${VERSION}.sqlite" 

// if the file does not exist 
if [ ! -f "${DB_FILE}" ]; then 
    // emit an error 
    echo "error: File \"${DB_FILE}\" not found!" >&2; 
    // and stop the build 
    exit 1 
fi 
+0

由於之前「歸順」的,這會是在編譯腳本中完成?當然,另一個可以刪除它。 – Larme

+0

@Larme您可以在構建或構建後的腳本中執行任何操作。 – Sulthan

+0

@Sulthan - 知道文件在提交之前就已經存在了...... – SpaceDog