如何使用完成处理程序填充列表 [SWIFT UI]
How to fill a list using Completion Handlers [SWIFT UI]
我想使用完成处理程序填充列表,问题是它在第一次执行时加载 nil 并在我尝试使用我的视图时标记错误,我的列表由完成处理程序填充。 . 有什么解决这个问题的建议吗?
这就是我尝试填写列表的方式
let invitationService: InvitationService = InvitationService()
@State private var invitationsList : [Appointment]?
init() {
let defaults = UserDefaults.standard
let userId = defaults.integer(forKey: "userId")
var appointmentList : [Appointment]?
invitationService.getInvitations(id: userId, completionHandler: { (appointment) in
if appointment != nil{
appointmentList = appointment
}
})
_invitationsList = State<[Appointment]?>.init(initialValue: (appointmentList))
}
如您所见,我需要填写我的列表,直到 InvitationService
请求结束,但如果我尝试将其放入代码中,我会得到一个
Escaping closure captures mutating 'self' parameter
错误
我是说
invitationService.getInvitations(id: userId, completionHandler: { (appointment) in
if appointment != nil{
appointmentList = appointment
self._invitationsList = State<[Appointment]?>.init(initialValue: (appointmentList))
}
})
我的观点
var body: some View {
NavigationView{
ZStack{
colors["LightGray"]?.edgesIgnoringSafeArea(.all)
Text("Hey").onAppear(){
let defaults = UserDefaults.standard
let userId = defaults.integer(forKey: "userId")
self.invitationService.getInvitations(id: userId) { (appointment) in
self.invitationsList = appointment
}
}
List {
ForEach(invitationsList!, id: \.self){invitation in
NavigationLink(destination: InvitationDetailView(invitation: invitation)){
HStack{
VStack(alignment: .center){
Text(invitation.startDate.prefix(2))
.font(.largeTitle)
Text(invitation.startDate[2..<6]).font(.subheadline)
}.padding()
Spacer().frame(width:UIScreen.main.bounds.width/15, alignment: .leading)
ImageView(withURL: invitation.imageProfile,widthValue: 80, heightValue: 80)
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
Spacer()
VStack(alignment: .leading){
Text(invitation.titleVisit)
.font(.headline)
.frame(width: UIScreen.main.bounds.width/3, alignment: .leading)
Text(invitation.typeVisit)
.font(.footnote)
Text(invitation.startDate.suffix(9))
.font(.caption)
}.padding()
}
.background(self.colors["White"])
.cornerRadius(25)
.foregroundColor(self.colors["Black"])
.shadow(radius: 2, x: 0, y: 0)
}
}.onDelete(perform: delete)
}
}.hideNavigationBar(text: "back".localized)
}
}
我正在尝试执行,当我这样做时 invitationsList
为零
我建议不要把这样的东西放到View.init
,因为SwiftUI视图是struct,value,并且可以在layout/rendering期间重新创建几次。更好的方法是 在 视图出现后执行此操作(或者,更好的是,根本不在视图层次结构之外)。
不管怎样,在这里查看是可能的方法
...
let invitationService: InvitationService = InvitationService()
@State private var invitationsList : [Appointment]? = nil
// nothing for init in this case
var body: some View {
Text("Any subview here")
.onAppear {
let defaults = UserDefaults.standard
let userId = defaults.integer(forKey: "userId")
self.invitationService.getInvitations(id: userId) { (appointment) in
self.invitationsList = appointment
}
}
}
我想使用完成处理程序填充列表,问题是它在第一次执行时加载 nil 并在我尝试使用我的视图时标记错误,我的列表由完成处理程序填充。 . 有什么解决这个问题的建议吗?
这就是我尝试填写列表的方式
let invitationService: InvitationService = InvitationService()
@State private var invitationsList : [Appointment]?
init() {
let defaults = UserDefaults.standard
let userId = defaults.integer(forKey: "userId")
var appointmentList : [Appointment]?
invitationService.getInvitations(id: userId, completionHandler: { (appointment) in
if appointment != nil{
appointmentList = appointment
}
})
_invitationsList = State<[Appointment]?>.init(initialValue: (appointmentList))
}
如您所见,我需要填写我的列表,直到 InvitationService
请求结束,但如果我尝试将其放入代码中,我会得到一个
Escaping closure captures mutating 'self' parameter
错误
我是说
invitationService.getInvitations(id: userId, completionHandler: { (appointment) in
if appointment != nil{
appointmentList = appointment
self._invitationsList = State<[Appointment]?>.init(initialValue: (appointmentList))
}
})
我的观点
var body: some View {
NavigationView{
ZStack{
colors["LightGray"]?.edgesIgnoringSafeArea(.all)
Text("Hey").onAppear(){
let defaults = UserDefaults.standard
let userId = defaults.integer(forKey: "userId")
self.invitationService.getInvitations(id: userId) { (appointment) in
self.invitationsList = appointment
}
}
List {
ForEach(invitationsList!, id: \.self){invitation in
NavigationLink(destination: InvitationDetailView(invitation: invitation)){
HStack{
VStack(alignment: .center){
Text(invitation.startDate.prefix(2))
.font(.largeTitle)
Text(invitation.startDate[2..<6]).font(.subheadline)
}.padding()
Spacer().frame(width:UIScreen.main.bounds.width/15, alignment: .leading)
ImageView(withURL: invitation.imageProfile,widthValue: 80, heightValue: 80)
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
Spacer()
VStack(alignment: .leading){
Text(invitation.titleVisit)
.font(.headline)
.frame(width: UIScreen.main.bounds.width/3, alignment: .leading)
Text(invitation.typeVisit)
.font(.footnote)
Text(invitation.startDate.suffix(9))
.font(.caption)
}.padding()
}
.background(self.colors["White"])
.cornerRadius(25)
.foregroundColor(self.colors["Black"])
.shadow(radius: 2, x: 0, y: 0)
}
}.onDelete(perform: delete)
}
}.hideNavigationBar(text: "back".localized)
}
}
我正在尝试执行,当我这样做时 invitationsList
为零
我建议不要把这样的东西放到View.init
,因为SwiftUI视图是struct,value,并且可以在layout/rendering期间重新创建几次。更好的方法是 在 视图出现后执行此操作(或者,更好的是,根本不在视图层次结构之外)。
不管怎样,在这里查看是可能的方法
...
let invitationService: InvitationService = InvitationService()
@State private var invitationsList : [Appointment]? = nil
// nothing for init in this case
var body: some View {
Text("Any subview here")
.onAppear {
let defaults = UserDefaults.standard
let userId = defaults.integer(forKey: "userId")
self.invitationService.getInvitations(id: userId) { (appointment) in
self.invitationsList = appointment
}
}
}