class 初始化问题
Problems with class initialization
我认为这是一个简单但令人头疼的问题。
当实际存在初始化程序时,我收到 ....has no initializers
错误。
这是我的代码:
(编辑:我已经更新了 PatientData class 以包含所有其他变量。我认为它们在解决问题方面没有什么不同,因此为了简洁起见,我将它们保留下来出。)
数据结构
class PatientData: Identifiable, ObservableObject
{
let id = UUID()
@Published var patientName: String = "Name"
@Published var patientAge: String = "Age"
@Published var patientDOB: String = "DOB"
@Published var patientPhone: String = "Phone"
@Published var patientAddress: PatientAddress
struct PatientAddress
{
var patientStreetAddress: String = "Street"
var patientCity: String = "City"
var patientState: String = "State"
var patientZip: String = "Zip"
init(patientStreetAddress: String, patientCity: String, patientState: String, patientZip: String)
{
self.patientStreetAddress = patientStreetAddress
self.patientCity = patientCity
self.patientState = patientState
self.patientZip = patientZip
}
}
@Published var facilityName: String = "Facility"
@Published var facilityRoom: String = "Room Number"
@Published var facilityFloor: String = "Floor"
@Published var facilityPhoneNumber: String = "Phone Number"
init(patientName: String, patientAge: String, patientDOB: String, patientPhone: String, patientAddress: PatientAddress, facilityName: String, facilityRoom: String, facilityFloor: String, facilityPhoneNumber: String)
{
self.patientName = patientName
self.patientAge = patientAge
self.patientDOB = patientDOB
self.patientPhone = patientPhone
self.patientAddress = patientAddress
self.facilityName = facilityName
self.facilityRoom = facilityRoom
self.facilityFloor = facilityFloor
self.facilityPhoneNumber = facilityPhoneNumber
}
init() {}
}
内容视图
struct ContentView: View
{
@ObservedObject var patient = PatientData()
...
}
注意:
PatientData()
相当于:
PatientData.init()
这意味着如果您想以这种方式创建 PatientData:
@ObservedObject var patient = PatientData()
您需要提供匹配的 init 方法(它可以为空,因为您的所有 @Published
属性都已经有默认值):
init() { }
编辑
查看您的完整代码,您的其中一个属性似乎没有初始值:
@Published var patientAddress: PatientAddress
只有当你的所有属性都已经初始化时,空的初始化才会起作用,这意味着你需要为你的 patientAddress
:
分配一些值
@Published var patientAddress = PatientAddress()
但是,要使其正常工作,您还需要在 PatientAddress
中添加一个空初始化:
struct PatientAddress {
...
init() {}
}
注意
如果没有所有这些初始化程序,您的所有代码都会简单得多。如果你只使用空的 init 方法,你不必声明它们(如果没有其他初始化器,它们是为结构自动生成的):
class PatientData: Identifiable, ObservableObject {
let id = UUID()
@Published var patientName: String = "Name"
@Published var patientAge: String = "Age"
@Published var patientDOB: String = "DOB"
@Published var patientPhone: String = "Phone"
@Published var patientAddress: PatientAddress = PatientAddress()
@Published var facilityName: String = "Facility"
@Published var facilityRoom: String = "Room Number"
@Published var facilityFloor: String = "Floor"
@Published var facilityPhoneNumber: String = "Phone Number"
}
extension PatientData {
struct PatientAddress {
var patientStreetAddress: String = "Street"
var patientCity: String = "City"
var patientState: String = "State"
var patientZip: String = "Zip"
}
}
此外,Swift 可以自动推断类型。您通常不必显式声明类型:
@Published var patientPhone = "Phone"
@Published var patientAddress = PatientAddress()
我认为这是一个简单但令人头疼的问题。
当实际存在初始化程序时,我收到 ....has no initializers
错误。
这是我的代码:
(编辑:我已经更新了 PatientData class 以包含所有其他变量。我认为它们在解决问题方面没有什么不同,因此为了简洁起见,我将它们保留下来出。)
数据结构
class PatientData: Identifiable, ObservableObject
{
let id = UUID()
@Published var patientName: String = "Name"
@Published var patientAge: String = "Age"
@Published var patientDOB: String = "DOB"
@Published var patientPhone: String = "Phone"
@Published var patientAddress: PatientAddress
struct PatientAddress
{
var patientStreetAddress: String = "Street"
var patientCity: String = "City"
var patientState: String = "State"
var patientZip: String = "Zip"
init(patientStreetAddress: String, patientCity: String, patientState: String, patientZip: String)
{
self.patientStreetAddress = patientStreetAddress
self.patientCity = patientCity
self.patientState = patientState
self.patientZip = patientZip
}
}
@Published var facilityName: String = "Facility"
@Published var facilityRoom: String = "Room Number"
@Published var facilityFloor: String = "Floor"
@Published var facilityPhoneNumber: String = "Phone Number"
init(patientName: String, patientAge: String, patientDOB: String, patientPhone: String, patientAddress: PatientAddress, facilityName: String, facilityRoom: String, facilityFloor: String, facilityPhoneNumber: String)
{
self.patientName = patientName
self.patientAge = patientAge
self.patientDOB = patientDOB
self.patientPhone = patientPhone
self.patientAddress = patientAddress
self.facilityName = facilityName
self.facilityRoom = facilityRoom
self.facilityFloor = facilityFloor
self.facilityPhoneNumber = facilityPhoneNumber
}
init() {}
}
内容视图
struct ContentView: View
{
@ObservedObject var patient = PatientData()
...
}
注意:
PatientData()
相当于:
PatientData.init()
这意味着如果您想以这种方式创建 PatientData:
@ObservedObject var patient = PatientData()
您需要提供匹配的 init 方法(它可以为空,因为您的所有 @Published
属性都已经有默认值):
init() { }
编辑
查看您的完整代码,您的其中一个属性似乎没有初始值:
@Published var patientAddress: PatientAddress
只有当你的所有属性都已经初始化时,空的初始化才会起作用,这意味着你需要为你的 patientAddress
:
@Published var patientAddress = PatientAddress()
但是,要使其正常工作,您还需要在 PatientAddress
中添加一个空初始化:
struct PatientAddress {
...
init() {}
}
注意
如果没有所有这些初始化程序,您的所有代码都会简单得多。如果你只使用空的 init 方法,你不必声明它们(如果没有其他初始化器,它们是为结构自动生成的):
class PatientData: Identifiable, ObservableObject {
let id = UUID()
@Published var patientName: String = "Name"
@Published var patientAge: String = "Age"
@Published var patientDOB: String = "DOB"
@Published var patientPhone: String = "Phone"
@Published var patientAddress: PatientAddress = PatientAddress()
@Published var facilityName: String = "Facility"
@Published var facilityRoom: String = "Room Number"
@Published var facilityFloor: String = "Floor"
@Published var facilityPhoneNumber: String = "Phone Number"
}
extension PatientData {
struct PatientAddress {
var patientStreetAddress: String = "Street"
var patientCity: String = "City"
var patientState: String = "State"
var patientZip: String = "Zip"
}
}
此外,Swift 可以自动推断类型。您通常不必显式声明类型:
@Published var patientPhone = "Phone"
@Published var patientAddress = PatientAddress()