admin管理员组文章数量:1316307
I'm trying to copy an object from an array, into another array. I've attempted using append, but the array size remains at 0. I've also tried using insert, but that also is not working for me. I have been searching for several days, but haven't been able to find a solution. How can I successfully add the object to the array?
struct Test: View {
@Query private var issues: [Issue]
@Environment(\.modelContext) private var context
@State private var newIssue: Issue?
@State var hasIdentifedIssue = false
@State var identifiedIssues: [Issue]?
var body: some View {
NavigationStack {
List {
Section() {
Text("Hello")
}
}
.onAppear {
for number in 0...issues.count {
if issues[number].status == IssueStatus.Identified {
identifiedIssues!.append(issues[number])
}
}
}
}
}
}
Edit: I the issue is with the append (I commented out the following):
.onAppear {
// for number in 0...issues.count - 1 {
// if issues[number].status == IssueStatus.Identified {
print("in here")
print(issues.count) // prints 1
identifiedIssues!.append(issues[0])
// }
// }
}
I'm trying to copy an object from an array, into another array. I've attempted using append, but the array size remains at 0. I've also tried using insert, but that also is not working for me. I have been searching for several days, but haven't been able to find a solution. How can I successfully add the object to the array?
struct Test: View {
@Query private var issues: [Issue]
@Environment(\.modelContext) private var context
@State private var newIssue: Issue?
@State var hasIdentifedIssue = false
@State var identifiedIssues: [Issue]?
var body: some View {
NavigationStack {
List {
Section() {
Text("Hello")
}
}
.onAppear {
for number in 0...issues.count {
if issues[number].status == IssueStatus.Identified {
identifiedIssues!.append(issues[number])
}
}
}
}
}
}
Edit: I the issue is with the append (I commented out the following):
.onAppear {
// for number in 0...issues.count - 1 {
// if issues[number].status == IssueStatus.Identified {
print("in here")
print(issues.count) // prints 1
identifiedIssues!.append(issues[0])
// }
// }
}
Share
Improve this question
edited Jan 30 at 4:23
CocaCola
asked Jan 30 at 2:14
CocaColaCocaCola
7733 gold badges10 silver badges22 bronze badges
2
- First thing you need to check is that if issues has any elements. Make sure issues.count is greater than 0. – azamsharp Commented Jan 30 at 2:58
- I did that, and yes it does. – CocaCola Commented Jan 30 at 3:19
1 Answer
Reset to default 1Your code crash because of 0...issues.count
.
Array indices start at 0
up until issues.count - 1
, that is
0..<issues.count
.
Try this instead, after testing for issues.isEmpty
:
declare
@State private var identifiedIssues: [Issue] = []
if !issues.isEmpty {
for number in 0..<issues.count { // <--- here
if issues[number].status == IssueStatus.identified {
identifiedIssues.append(issues[number])
}
}
}
Better to use this:
for number in issues.indices { // <--- here
if issues[number].status == IssueStatus.identified {
identifiedIssues.append(issues[number])
}
}
本文标签: swiftuiUnable to add object to arrayStack Overflow
版权声明:本文标题:swiftui - Unable to add object to array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741989110a2408863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论