【发布时间】:2020-07-06 16:45:32
【问题描述】:
struct AppointmentView: View
{
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Appoint.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Appoint.title , ascending: true)])
var appointments_title: FetchedResults<Appoint>
@State var showAddDate = false
@State var sectionState: [Int: Bool] = [:]
var body: some View
{
NavigationView
{
List
{
ForEach(appointments_title)
{
order in
HStack
{
Text("\(order.title)").font(.headline)
Spacer()
Text("\(order.date, formatter: ContentView.self.taskDateFormat)").font(.headline)
}.contentShape(Rectangle())
.onTapGesture
{
self.sectionState[order] = !self.isExpanded(order)
}
if self.isExpanded(order)
{
Text("\(order.description)”)
}
}
}
.navigationBarTitle("My Appointment")
.navigationBarItems(trailing: Button(action: {self.showAddDate = true}, label: {Image(systemName: "plus.circle").resizable().frame(width: 32, height: 32, alignment: .center)}))
.sheet(isPresented: $showAddDate) {AddDate().environment(\.managedObjectContext, self.managedObjectContext)}
}
}
func isExpanded(_ order:Int) -> Bool
{
sectionState[order] ?? false
}
}
当我尝试运行此代码时,我会收到此错误消息;
无法将“FetchedResults ”类型的值转换为预期
参数类型'Range '
有没有办法将 'FetchedResults ' 转换为 'Range ' ?
【问题讨论】: