Reputation: 321
I am initializing the variables for SQLite database in the WidgetData
class but there is an error I have got right now,
Return from initializer without initializing all stored properties
but previously it was running successfully.
I have followed this but even I have added them empty value still getting the error, help to resolve this.
Here is what I have done so far:
import Foundation
class WidgetData {
var id: Int64?
var name: String
var entered : String
var address: String
var formid : Int64?
var formname : String
var formdescription : String
var formcategory : String
init(id: Int64) {
self.id = id
name = ""
entered = ""
address = ""
}
init(formid: Int64) {
self.formid = formid
formname = ""
formdescription = ""
formcategory = ""
}
init(id: Int64, name: String, entered: String, address: String) {
self.id = id
self.name = name
self.entered = entered
self.address = address
}
init(formid: Int64, formname : String, formdescription : String, formcategory : String) {
self.formid = formid
self.formname = formname
self.formdescription = formdescription
self.formcategory = formcategory
}
}
Upvotes: 1
Views: 1600
Reputation: 10105
The problem is that the following properties:
var name: String
var entered : String
var address: String
var formname : String
var formdescription : String
var formcategory : String
are not optional, so they must be initialized:
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.
Hence you could assign them a default value, so your code might be:
import Foundation
class WidgetData {
var id: Int64?
var name: String = ""
var entered : String = ""
var address: String = ""
var formid : Int64?
var formname : String = ""
var formdescription : String = ""
var formcategory : String = ""
init(id: Int64) {
self.id = id
}
init(formid: Int64) {
self.formid = formid
}
init(id: Int64, name: String, entered: String, address: String) {
self.id = id
self.name = name
self.entered = entered
self.address = address
}
init(formid: Int64, formname : String, formdescription : String, formcategory : String) {
self.formid = formid
self.formname = formname
self.formdescription = formdescription
self.formcategory = formcategory
}
}
Upvotes: 1