如何在 SwiftUI 中显示数组的长度

sch*_*hne 2 xcode swift swiftui

我正在尝试创建一个小型社交媒体应用程序。到目前为止一切工作正常,但是当我试图显示 an 的长度(这保存了 a中Array的朋友)时,我收到错误UserProfilView"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)
        }
    )
Run Code Online (Sandbox Code Playgroud)
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
    }
    
}
Run Code Online (Sandbox Code Playgroud)

jn_*_*pdx 6

您正在尝试发送一个Intto Text,它需要String在其初始值设定项中包含 a 。

您可以将变量插入String

Text("\(appUser.friends.count)")
Run Code Online (Sandbox Code Playgroud)

或者,您可以将 传递IntString初始值设定项:

Text(String(appUser.friends.count))
Run Code Online (Sandbox Code Playgroud)