SwiftUI-PersonNameComponents()无法使用变量初始化

时间:2020-10-05 15:08:50

标签: ios swift swiftui

我正在使用Xcode版本12.0.1(12A7300)

我已从Apple下载此代码,以测试使用PersonNameComponents制作Monogram。

https://developer.apple.com/documentation/foundation/formatter/displaying_human-friendly_content

我已经在Xcode中创建了一个新项目,以从Apple的代码中重做Monogram.swift。

我的问题是:我无法在项目中使用变量初始化PersonNameComponents,但在Apple项目中却能正常工作。即使我复制该类并将其粘贴到我的项目中,也会收到错误消息。

编译器错误:“传递给不带参数的调用的参数”。 compilor error screenshot

struct Monogram_Previews: PreviewProvider {
static var previews: some View {
    VStack {
        let sampleName = PersonNameComponents(familyName: "मिश्र", givenName: "करन")
        Monogram(nameComponents: sampleName, color: .orange)
            .padding(.bottom, 20)
        Monogram(nameComponents: sampleName, color: .blue, sideLength: 100)
            .padding(.bottom, 20)
        Monogram(nameComponents: sampleName, color: .pink, sideLength: 250)
    }
}

}

谢谢。

1 个答案:

答案 0 :(得分:1)

错误非常清楚,PersonNameComponentsFormatter没有不带参数的初始化程序。您可能忘记了在项目中包含NamesView.swift文件中提供的自定义init:

extension PersonNameComponents {
    init(namePrefix: String? = nil,
         familyName: String? = nil,
         middleName: String? = nil,
         givenName: String? = nil,
         nameSuffix: String? = nil,
         nickname: String? = nil,
         phoneticRepresentation: PersonNameComponents? = nil) {
        self.init()
        self.namePrefix = namePrefix
        self.familyName = familyName
        self.middleName = middleName
        self.givenName = givenName
        self.nameSuffix = nameSuffix
        self.nickname = nickname
        self.phoneticRepresentation = phoneticRepresentation
    }
}