如何在swiftui的可观察对象中动态定义一个对象?



我刚开始学习swiftui。

我有一种情况,我必须在可观察对象中定义一个对象的数组(假设对象是USER,那么object的数组将是"[USER]")。这个可观察对象是一个ViewModel。有一个API调用在任何其他视图中初始化这个ViewModel。

我必须初始化这个[User]对象后,这个ViewModel将被初始化。我必须把这个[USER]在ViewModel中,因为这个对象[USER]将被其他视图上的TextField修改。

由于几个编译时错误,我无法在API调用响应后初始化此[USER]对象。

struct User: Identifiable {
var profileName: String = ""
var profileId: String = ""   
var firstname: String = ""
var lastname: String = ""
}

class ViewModel: ObservableObject {
@Published var document: Document?
@Published var users = [User]()
var idValue: String
init (idValue: String) {
idValue = idValue
}

func getDetails() {

APIService.shared.getDocumentDetails(idValue: idValue) { document in
DispatchQueue.main.async {
self.document = document

self.setEndUserValues()
}
}
}

func setEndUserValues() {

var users = [User]()

if let document = document, let subjects = document.subjects {
ForEach(0..<subjects.count, id: .self) { index in
let profile = subjects[index]
var user = User(id: index)

user.profileName = profile.profileName ?? ""
user.profileId = profile.profileId ?? ""
user.firstname = profile.firstname ?? ""
user.lastname = profile.lastname ?? ""
users.append(user)
}
}
}

我得到类型'()'不能符合'视图'错误。而且,响应是嵌套的,所以我没有提到更多的属性。请帮我实现这个目标。

您可能会发现下面的代码比固定的代码工作得更好:

struct User: Identifiable {

var profileName: String
var id: String // In order to conform to Identifiable, you have to have
// a variable named "id"
var firstName: String // We write Swift variables in camelcase. 
var lastName: String  // You can even see that these get flagged as a spelling error. 
}
class ViewModel: ObservableObject {

@Published var document: Document?

@Published var users = [User]()
var idValue: String

init(idValue: String) { // I am not sure of the purpose of initializing the
// ViewModel with an "id". You shouldn't have multiple
// copies of this in your app.
self.idValue = idValue
}

func getDetails() {
APIService.shared.getDocumentDetails(idValue: idValue) { document in
DispatchQueue.main.async {
self.document = document

self.setEndUserValues()
}
}
}

func setEndUserValues() {
if let document = document, let subjects = document.subjects {
for subject in subjects { // You have an array of Subjects. Use it directly.
// This way of initializing creates your User and appends it to the
// array of Users in one step.
users.append(User(
profileName: subject.profileName,
id: subject.profileId,
firstName: subject.firstname,
lastName: subject.lastname))
}
}
}
}

我想用一分钟的时间来展示我在上面修复的代码中的一些问题:

func setEndUserValues() {

var users = [User]() // You are creating a local array here. This is not your
// "@Published var users"

if let document = document, let subjects = document.subjects {
for index in 0..<subjects.count { // This becomes unnecessary when you don't
// need the index.
let profile = subjects[index] // This becomes unnecessary.
var user = User(id: index) // According to your User struct, this doesn't
// compile. You don't have an initializer that
// accepts an "id: Int"

user.profileName = profile.profileName ?? ""
user.profileId = profile.profileId ?? ""
user.firstname = profile.firstname ?? ""
user.lastname = profile.lastname ?? ""
users.append(user) // This is your local array of users. This doesn't
// actually go anywhere, and you don't actually
// fill your ViewModel.users array
}
}
}

最新更新