我想更改从核心数据中检索对象的列表中的行顺序。移动行有效,但问题是我无法保存更改。我不知道如何保存更改后的 CoreData 对象的索引。
这是我的代码:
核心数据类:
public class CoreItem: NSManagedObject, Identifiable{
@NSManaged public var name: String
}
extension CoreItem{
static func getAllCoreItems() -> NSFetchRequest <CoreItem> {
let request: NSFetchRequest<CoreItem> = CoreItem.fetchRequest() as! NSFetchRequest<CoreItem>
let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
request.sortDescriptors = [sortDescriptor]
return request
}
}
extension Collection where Element == CoreItem, Index == Int {
func move(set: IndexSet, to: Int, from managedObjectContext: NSManagedObjectContext) {
do {
try managedObjectContext.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error …Run Code Online (Sandbox Code Playgroud) 如何在用 SwiftUI 编写的 Catalyst 应用程序中添加触控栏支持?
例如,如果我想在视图中显示一个按钮:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
#if targetEnvironment(macCatalyst)
Text("macOS")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.focusable()
.touchBar {
Button(action: {
print("tapped")
}) {
Text("TestButton")
}
}
#endif
Text("iOS")
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在 macOS 应用程序中使用它,它可以工作,但如果我在 Catalyst 中使用它并添加 targetEnvironment,则会出现错误:
“focusable(_:onFocusChange:)”在 iOS 中不可用,“touchBar(content:)”在 iOS 中不可用
谢谢你的帮助。