Return from Initializer Without Initializing All Stored Properties

Return from initializer without initializing all stored properties - SwiftUI Binding error

import SwiftUI

struct ResultViewSilence: View {

///@State needs to be initialized here because it CAN store values
///https://developer.apple.com/documentation/swiftui/state
@State var isDismissView1: Bool = false

///@Binding creates a 2 way connection does NOT store values needs a parent that stores values
///https://developer.apple.com/documentation/swiftui/binding
@Binding var isDismissView2: Bool

var hasSilence: Bool

//let photolibrary = PhotoLibrary() //No code provided

init(hasSilence: Bool, isDismissView2: Binding<Bool> ) {
self.hasSilence = hasSilence
self._isDismissView2 = isDismissView2
}
var body: some View {
VStack{
Text("isDismissView1 = " + isDismissView1.description)
Text("isDismissView2 = " + isDismissView2.description)
}
}
}
struct ParentResultViewSilence: View {
//Parent that can store values
@State var isDismissView2: Bool = true
var body: some View {
ResultViewSilence(hasSilence: false, isDismissView2: $isDismissView2)
}
}
struct ResultViewSilence_Previews: PreviewProvider {
static var previews: some View {
//ResultViewSilence(hasSilence: false, isDismissView2: .constant(true))
ParentResultViewSilence()
}
}

Return from initializer without initializing all stored properties Swift Xcode 10.0

If you create an initializer, you need to specify the value for all stored properties in the initializer, you cannot use the default values of your properties. So even if you declare your properties as Optional, you need to assign nil value to them in your initializer if you want them to be nil.

Unrelated to your issue, but there's no need to declare CodingKeys if all of your property names match your JSON keys and there's also no need to manually write the init(from:) initializer, the compiler can automatically synthesise that for you in your simple case. However, you should conform to the Swift naming convention, which is lowerCamelCase for variable names (including enum cases), so rename your properties accordingly and then you'll need CodingKeys.

Be aware that a lot of your types don't actually make sense. Why are the variables called count Strings? If they're coming as Strings from the backend, convert them to Ints in init(from:). Also, in your init(doctor:) it would make sense to actually add doctor to your doctors array.

struct Conversation : Codable {

let chatId: String?
let id: String?
let name: String?
let profilePic: String?
let lastMessageFrom: String?
let message: String?
let time: String?
let unreadCount: String?
let memberCount: String?
var type: ChatType = .single
var doctors:[Doctors]?

enum CodingKeys: String, CodingKey {
case chatId = "chat_id"
case id
case name
case profilePic = "profile_pic"
case lastMessageFrom = "last_message_from"
case message
case time
case unreadCount = "unread_count"
case memberCount = "member_count"
case doctors
}

init(doctor:Doctors) {
self.id = doctor.doctorId
self.profilePic = doctor.doctorPic
self.type = .single
self.chatId = nil
self.name = nil
self.lastMessageFrom = nil
self.message = nil
self.time = nil
self.unreadCount = nil
self.memberCount = nil
self.doctors = [doctor]
}
}

Error: Return from initializer without initializing all stored properties in SwiftUI Project?

Normally when you created a View, since it's a struct, Xcode synthesizes initializers for you. This means that you pass a Binding in as a parameter and it automatically gets set for you.

In this case, since you've definite your own init, you also have to take that Binding parameter and initialize your own property.


struct MyView : View {
@Binding var tabSelection: Int

init(tabSelection: Binding<Int>) {
_tabSelection = tabSelection //<-- Here (have to use the underscore because of the `@Binding` -- see the link later in the post

UINavigationBar.appearance().barTintColor = .clear
UINavigationBar.appearance().backgroundColor = .clear; UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().shadowImage = UIImage()
}

var body: some View {
Text("Hello, world!")
}
}

See also: SwiftUI: How to implement a custom init with @Binding variables

Swift Error Message - Return from initializer without initializing all stored properties

In your ContentView, the following line is not initialized in your init:

var flights: FetchedResults<Flight>

Return from initializer without initializing all stored properties error in SwiftUI

You haven't initialized shouldPopToRootView.

Return from initializer without initializing all stored properties swift ios

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
}
}


Related Topics



Leave a reply



Submit