Home > Software engineering >  How to display the length of an Array in SwiftUI
How to display the length of an Array in SwiftUI

Time:03-01

I am trying to create a small social media app. Everything works fine so far, but when I am trying to show the length off an Array (which saves the friends of a User in the ProfilView), I am getting the Error "No exact matches in call to initializer"

Capsule()
    .frame(width: 110, height: 30)
    .offset(x: 40)
    .foregroundColor(.blue)
    .overlay(
        HStack {
            Image(systemName: "person.3.fill")
                .foregroundColor(.white)
                .position(x: 120, y: 15)
            Text(appUser.friends.count)
                .foregroundColor(.white)
                .position(x: 35, y: 15)
                .frame(width: 70, height: 30, alignment: .trailing)
        }
    )
class User: ObservableObject{
    
    @Published var username: String = ""
    @Published var name: String = ""
    var password: String = ""
    @Published var email: String = ""
    @Published var beschreibung: String = ""
    @Published var profilBild: UIImage?
    
    @Published var friends = [User]()
    
    
    init(_ username: String, _ password: String, _ email: String, _ name: String) {
        self.username = username
        self.password = password
        self.email = email
        self.name = name
    }
    
}

CodePudding user response:

You're trying to send an Int to Text, which expects a String in its initializer.

You could interpolate the variable into a String:

Text("\(appUser.friends.count)")

Or, you could pass the Int into a String initializer:

Text(String(appUser.friends.count))
  • Related