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即使文件不在捆绑包中,也会始终显示。

4

1 回答 1

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
于 2017-02-21T21:01:30.060 回答