Gino Sesia
Gino Sesia

Reputation: 393

How to count elements from a JSON file in swift?

I am trying to count how many comments each individual post has from a JSON file.

The post and comments are linked via ids. the each post has an id and each comment has a post if to distinguish which post it belongs with.

I have displayed all of the posts and I want to display a label on the post showing the number of comments that the specific post has.

So far I have managed to display a Text block for each comment on the post so I can see that the logic is working and how many comments each post has by counting how many times the text block is repeated but this is not what I want, I want to only display one Text block with a number showing the count.

This is what I have so far: The text that says "here to show its working" is just there so you can see that it is working and will be removed (i also know that because it is inside the foreach loop it is displaying over and over again depending on how many comments there are)

so my question is how do i somehow count these comments and display a number?:

var body: some View {
    ZStack {
        HStack {
            VStack(alignment: .leading) {
                Text(post.title)
                    .foregroundColor(.white)
                    .font(.system(size: 22, weight: .medium, design: .rounded))
                ForEach(comments) { comment in
                    if comment.postId == post.id {
                        Text("here to show its working")
                            .foregroundColor(.pink)
                            .font(.system(size: 16, weight: .light, design: .rounded))

                    }
                }
                Text("comments")
                    .foregroundColor(.pink)
                    .font(.system(size: 16, weight: .light, design: .rounded))
            }.padding()
            Spacer()
        }
    }//: VStack
    .onAppear {
        commentApi().getComments { (comments) in
            self.comments = comments
        }
    }
    .background(
        LinearGradient(gradient: Gradient(
            colors: [
                Color.purple, Color.blue
            ]),
            startPoint: .topLeading,
            endPoint: .bottomTrailing
        )
    )
    .shadow(color: .black.opacity(0.1), radius: 15, x: 0, y: 0)
    .cornerRadius(15)
}//: Body

This is what it looks like:

enter image description here

And this is how I want it to look and work (i just hard coded "5 comments" to show what I want to achieve):

enter image description here

Upvotes: 0

Views: 192

Answers (1)

how about something like this:

func commentsCount(_ postId: Int) -> String {
    return String(comments.filter{$0.postId == postId}.count)
}

var body: some View {
    ZStack {
        HStack {
            VStack(alignment: .leading) {
                Text(post.title)
                    .foregroundColor(.white)
                    .font(.system(size: 22, weight: .medium, design: .rounded))
                
                Text(commentsCount(post.id) + " comments")
                    .foregroundColor(.pink)
                    .font(.system(size: 16, weight: .light, design: .rounded))
   
            }.padding()
            Spacer()
        }
    }
    .onAppear {
        commentApi().getComments { (comments) in
            self.comments = comments
        }
    }
    .background(
        LinearGradient(gradient: Gradient(
            colors: [Color.purple, Color.blue]),startPoint: .topLeading,endPoint: .bottomTrailing))
    .shadow(color: .black.opacity(0.1), radius: 15, x: 0, y: 0)
    .cornerRadius(15)
}

Upvotes: 3

Related Questions