ios - 如何从 IOS 小部件中的领域数据库中获取数据?迅速
问题描述
我有一个当前正在运行的应用程序的现有领域数据库。
我想从数据库中获取数据以显示在小部件中。但是,我收到错误“在范围内找不到类型'项目'。它拼写正确。
import WidgetKit
import SwiftUI
import Intents
import RealmSwift
struct Provider: IntentTimelineProvider {
let realm = try! Realm()
var todoItems: Results<Item>? //cannot find type 'Item' in scope
我已经为这两个目标安装了领域 swift,并设置了一个应用程序组。我还尝试了以下代码:Share Realm Data with WatchOS以及 Swift 5 的更新版本。
这是数据模型的主应用程序目标中的代码:
import Foundation
import RealmSwift
class Item: Object {
@objc dynamic var title: String = ""
@objc dynamic var done: Bool = false
@objc dynamic var dateCreated: Date?
var parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}
这是应用程序委托
import UIKit
import RealmSwift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//MARK: Realm Location Setup - Commented out because it didn't work :(
// if var directory: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.lachlanosborne.countdownwidget") {
// directory.appendPathComponent("db.realm", isDirectory: true)
// let config = Realm.Configuration(fileURL: directory, schemaVersion: 1)
// Realm.Configuration.defaultConfiguration = config
// }
//MARK: Prints
//print(Realm.Configuration(fileURL: sharedRealmURL))
print("app running")
//MARK: Realm Setup
do {
_ = try Realm()
} catch {
print("Error creating realm")
}
return true
}
}
解决方案
推荐阅读
- python - 使用 Python 从 CSV 文件中的指定行和列中提取值。不能使用 CSV 模块或 pandas 模块
- google-cloud-platform - gsutil mb -c 标准 gs://gcpassignmentapp 给出错误 未给出命令 选择 -b、-d、-e 或 -r 之一来执行某项操作
- python - numpy 向数组添加维度
- asp.net-core - Asp.net Core 3.1 在启动时解析 Autofac 容器并进行一些初始化
- javascript - 用户不存在时如何处理问题
- c++ - 如何从 CUDA 中的推力 device_ptr 偏移?
- api - 未找到纽曼命令
- javascript - React Materialise CSS 全屏高度
- variables - Ansible 循环查找问题
- java - 如何在 10 行和 5 列中显示奇数?