TL;DR: 在 VSCode 或 Cursor 中测试
Core Data
时需确保使用 Xcode 提供的Swift
工具链,并在.vscode/settings.json
或.code-workspace
文件中正确配置DEVELOPER_DIR
。此外,需手动将.xcdatamodeld
转换为.momd
,因为 VSCode 无法自动处理该步骤。转换可通过xcrun momc
脚本完成,确保测试运行时模型文件可被正确加载。
背景
随着 Swift for VSCode 插件的发展,开发者越来越倾向于在 VSCode 或 Cursor 中进行 Swift 开发。但面对如 Core Data 这类苹果生态特有的框架时,测试过程往往遇到困难。本文介绍在该环境中成功测试 Core Data 的关键配置方法。
使用正确的 Swift 工具链
macOS 上可能安装有多个 Swift 工具链版本,部分工具链(如 swiftly 安装的开源版本)不支持 Core Data 等闭源框架。确保使用 Xcode 提供的工具链至关重要。
查找并设置工具链路径
使用 where swift
找到所有安装路径:
Bash
where swift
/Users/yangxu/.swiftly/bin/swift
/usr/bin/swift
运行以下命令确认 Xcode 工具链信息:
Bash
/usr/bin/swift -v
# 输出示例
Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0
/Applications/Xcode-16.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-help intro
Welcome to Swift!
Subcommands:
swift build Build Swift packages
swift package Create and work on packages
swift run Run a program from a package
swift test Run package tests
swift repl Experiment with Swift code interactively
Use `swift --version` for Swift version information.
Use `swift --help` for descriptions of available options and flags.
Use `swift help <subcommand>` for more information about a subcommand.
将 Xcode 工具链路径配置至 .vscode/settings.json
:
JSON
{
"swift.path": "/usr/bin/",
"swift.swiftEnvironmentVariables": {
"DEVELOPER_DIR": "/Applications/Xcode-16.3.0.app"
},
"lldb.library": "/Applications/Xcode-16.3.0.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB",
"lldb.launch.expressions": "native"
}
或在 .code-workspace
文件中统一配置:
JSON
{
"folders": [
{
"path": "CorDataCodes"
}
],
"settings": {
"swift.path": "/usr/bin/",
"swift.swiftEnvironmentVariables": {
"DEVELOPER_DIR": "/Applications/Xcode-16.3.0.app"
},
"lldb.library": "/Applications/Xcode-16.3.0.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB",
"lldb.launch.expressions": "native"
}
}
将 xcdatamodeld 手动转换为 momd
Xcode 会在编译时自动将 xcdatamodeld
转换为 momd
,但 VSCode 不具备此功能,即使资源声明为 .process
也无效:
Swift
resources: [
.process("Resources"),
]),
需手动执行转换,以下是对应脚本示例(放在 Package 根目录):
Bash
#!/bin/bash
# 换成你的 模块、目标、数据模型 名称
PACKAGE_NAME="CorDataCodes"
Target_Name="DataHandler"
MODEL_NAME="Model"
SOURCE_MODEL="Sources/$Target_Name/Resources/$MODEL_NAME.xcdatamodeld"
TARGET_DIR=".build/arm64-apple-macosx/debug/${PACKAGE_NAME}_${Target_Name}.bundle/$MODEL_NAME.momd"
if [ ! -d "$SOURCE_MODEL" ]; then
echo "Error: Source model file not found: $SOURCE_MODEL"
exit 1
fi
mkdir -p "$TARGET_DIR"
xcrun momc "$SOURCE_MODEL" "$TARGET_DIR"
if [ $? -eq 0 ]; then
echo "Core Data model conversion successful!"
echo "Source file: $SOURCE_MODEL"
echo "Target file: $TARGET_DIR"
else
echo "Conversion failed!"
exit 1
fi
运行命令:
Bash
./xcmodel2momd.sh
#输出示例
Model.xcdatamodel: note: Model Model version checksum: 0wr8l/hdOcRIaAEOGJvaYn7eMvrqCq3uDvCCQiI3mSQ=
Core Data model conversion successful!
Source file: Sources/Persistent/Resources/Model.xcdatamodeld
Target file: .build/arm64-apple-macosx/debug/CorDataCodes_DataHandler.bundle/Model.momd
每次修改模型后都应重新执行该脚本。
效果
配置完成后,可直接点击测试按钮运行涉及 Core Data 的单元测试: