使用SiftData使用Siri的Swift Appintents/快捷方式提示了多个新的记录字段。将记录保存到用户响应

问题描述 投票:0回答:1
@Model class Thing { var name: String = "" var comment: String = "no comment" var recordType: String = "0" var count: Int = 0 init(name: String, comment: String, recordType: String, count: Int) { self.name = name self.comment = comment self.recordType = recordType self.count = count } }//class struct CreateNewThing: AppIntent { static var title: LocalizedStringResource = "Create New Thing" static var description = IntentDescription("Opens the app and moves to the Add Thing screen.") static var openAppWhenRun: Bool = true @AppStorage("navigateToThingAddView") private var navigateToThingAddView: Bool = false @MainActor func perform() async throws -> some IntentResult { navigateToThingAddView = true return .result() } }//create new thing struct ThingAddDialog: AppIntent { static var title: LocalizedStringResource = "Add Thing Dialog" static var description = IntentDescription("Prompt the user for a name and comment for a new Thing.") static var openAppWhenRun: Bool = true @Parameter(title: "Name", default: "") var name: String @Parameter(title: "Comment", default: "No comment") var comment: String @MainActor func perform() async throws -> some ProvidesDialog & ShowsSnippetView { // Logic to navigate to the Add Thing dialog view //navigateToThingDialog = true let dialog = IntentDialog( full: "What is the name of the new Thing?", supporting: "I have opened the Add Thing Dialog.") let answer = $name.needsValueError("Provide a name") name = answer.description saveThing(name: name, comment: comment) return .result(dialog: dialog) } @MainActor private func saveThing(name: String, comment: String) { let newThing = Thing(name: name, comment: comment, recordType: "0", count: 0) // Add newThing to SwiftData let container = try! ModelContainer(for: Thing.self) container.mainContext.insert(newThing) try? container.mainContext.save() } } struct ThingShortcuts: AppShortcutsProvider { static var shortcutTileColor = ShortcutTileColor.navy static var appShortcuts: [AppShortcut] { AppShortcut( intent: CreateNewThing(), phrases: [ "Create a new Thing in \(.applicationName)" ], shortTitle: "Create a Thing", systemImageName: "document.badge.plus" ) AppShortcut( intent: ThingAddDialog(), phrases: ["Add Dialog in \(.applicationName)"], shortTitle: "Add Thing Dialog", systemImageName: "plus.square" ) }//var }//thing shortcuts

任何指导将不胜感激。 Xcode 16.1,iOS18.1

trory制作一个新的类对象并在appintent上调用。
您需要在课程中构建新的模型上下文:

final class ModelManager: NSObject, ObservableObject { private var models: [any PersistentModel.Type] = [ Thing.self, ] lazy public var container: ModelContainer = { let schema = Schema(models) let conf = ModelConfiguration( schema: schema, isStoredInMemoryOnly: false) do { return try ModelContainer(for: schema, configurations: [conf]) } catch { fatalError( "Could not create ModelContainer: \(error.localizedDescription)" ) } }() lazy public var context: ModelContext = ModelContext(container) func someFunction() { // try to use model here on another function! try! context.delete(model: Thing.self) try! context.save() } }

ios swift sirishortcuts appintents
1个回答
0
投票

func perform() async throws -> some IntentResult & ProvidesDialog { let manager = ModelManager() manager.someFunction() //code... }

(忽略不良英语XD)
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.