ios - 使用 Swift 捆绑领域文件的正确方法
问题描述
我试图将我的预填充Realm
文件与我的应用程序捆绑在一起。我复制了 if 并在 Build Phase 添加了复制包资源区域。但是每次启动应用程序时,我都无法提取数据。当我查看我的应用程序在设备上创建的文件时,那里的类是空的。
这是我获取对象的方式
var allTrees: Results<trees>?
allTrees = realm.objects(trees.self)
print(allTrees?.count ?? 0) // always zero
我的AppDelegate
设置
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
let bundlePath = Bundle.main.path(forResource: "default", ofType: ".realm")
let destPath = Realm.Configuration.defaultConfiguration.fileURL?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
//File exist, do nothing
print(destPath ?? "if loop")
} else {
do {
//Copy file from bundle to Realm default path
try fileManager.copyItem(atPath: bundlePath!, toPath: destPath!)
} catch {
print(error)
}
}
return true
}
我的模特班
class trees: Object {
@objc dynamic var turkishName = ""
@objc dynamic var latinName = ""
@objc dynamic var seedTpye = true
@objc dynamic var leafType = true
@objc dynamic var botanicProperties = ""
@objc dynamic var spreadingArea = ""
}
解决方案
推荐阅读
- python-3.x - 使用不同数量的过滤器参数构造 SQLite 查询的正确方法
- haskell - Vigenere Cipher 未正确编码
- azure - 如何从 Azure 数据工厂的 blob 存储中解压缩 .gz 文件?
- react-native - 使用 Lottie 的 React Native Flatlist 自定义刷新控件出现故障
- arrays - 在数组及其索引中找到最小值 - 完全是初学者,因此非常感谢任何帮助
- .net - 在 .net framework WCF 中引用 .net core dll
- linux - 将 Go 应用程序从 Windows 编译到 Linux 时出现问题
- mysql - HSQLDB 是否支持 MySQL 兼容模式下枚举的默认值?
- python - Plotly:条形图不透明度随时间范围的变化而变化
- powershell - 从 Azure DevOps powershell 任务调用 sqlcmd 失败