如何在 swiftui 中动态定义可观察对象内的对象?
How to dynamically defined an object inside observable objects in swiftui.?
我是 swiftui 的新手,正在学习它。
我有一种情况,我必须在可观察对象中定义一个对象数组(假设对象是 USER,所以对象数组将是“[USER]”)。
这个可观察对象是一个 ViewModel。在初始化此 ViewModel 的任何其他视图中有一个 API 调用。
只有在初始化此 ViewModel 之后,我才必须初始化此 [User] 对象。我必须将此 [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)
}
}
}
我收到类型“()”无法符合 'View' 的错误。另外,响应是相当嵌套的,所以我没有提到更多的属性。请帮我实现这个目标。
您可能会发现以下代码比您的固定代码效果更好:
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
}
}
}
我是 swiftui 的新手,正在学习它。
我有一种情况,我必须在可观察对象中定义一个对象数组(假设对象是 USER,所以对象数组将是“[USER]”)。 这个可观察对象是一个 ViewModel。在初始化此 ViewModel 的任何其他视图中有一个 API 调用。
只有在初始化此 ViewModel 之后,我才必须初始化此 [User] 对象。我必须将此 [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)
}
}
}
我收到类型“()”无法符合 'View' 的错误。另外,响应是相当嵌套的,所以我没有提到更多的属性。请帮我实现这个目标。
您可能会发现以下代码比您的固定代码效果更好:
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
}
}
}