最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - Unable to add object to array - Stack Overflow

programmeradmin0浏览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])
//                    }
//                }
            }

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
Add a comment  | 

1 Answer 1

Reset to default 1

Your 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])
    }
}
发布评论

评论列表(0)

  1. 暂无评论