UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Submitting Int Into Coredata always 0

Forums > SwiftUI

I have a form that submits info into coredata.

The strings go in fine but the integer is always 0.

The code i have is as follows:


import SwiftUI

struct NewHumidor: View {
    //give access to the data storage
    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment (\.presentationMode) var presentationMode

    @State var humName = ""
    @State var humLoc = ""
    @State var cigCnt: Int16? = nil

    var body: some View {
        NavigationView{
            Form{
                Section(header: Text("Humidor Details")){
                    TextField("Humidor Name", text: $humName)
                    TextField("Humidor Location", text: $humLoc)
                    TextField("Cigar Count", value: $cigCnt, formatter: NumberFormatter())

                }
                Button(action: {
                    //makes sure that name is not blank
                    guard self.humName != "" else {return}
                    let newHum = Humidors(context: self.managedObjectContext)
                    newHum.name = self.humName
                    newHum.location = self.humLoc
                    newHum.cigarcount = self.cigCnt

                    newHum.id = UUID()
                    do {
                        try self.managedObjectContext.save()
                        print("Humidor Created")
                        self.presentationMode.wrappedValue.dismiss()
                    } catch {
                        print(error.localizedDescription)
                    }

                }){
                    Text("Save New Humidor")
                }
            .navigationBarTitle("New Humidor")
            }
        }
    }
}

struct NewHumidor_Previews: PreviewProvider {
    static var previews: some View {
        NewHumidor()
    }
}

I am getting the error message:

Value of optional type 'Int16?' must be unwrapped to a value of type 'Int16'

If anyone could point me in the right direction, i would be very greatfull.

Thanks

Adam

3      

@State var cigCnt: Int16? = nil

cigCnt is optional, and the TextField cannot take an optional.

@State var cigCnt: Int16 = 0

Will make the error go away and work as intended. If cigCnt needed to be optional, that would need to be addressed.

3      

Assuming that it does need to be optional (because i dont want a preselected value), what steps would i need to take here. Apologies in advance, i am brand new to this.

Appreciate your help in advance.

3      

My experience with CoreData and SwiftUI is that the TextField and the NumberFormatter() just doesn't work with Int16. You could cast your value

newHum.cigarcount = Int16(self.cigCnt) ?? 0

and set it to 0 if none was entered.

3      

That is still giving me an error unfortunately.

Chnaging that line to your suggestion give me the following error on that line.

Initializer 'init(_:radix:)' requires that 'Int16?' conform to 'StringProtocol'

3      

Also, would you suggest that Int would be better to use than Int16? Or do they act the same in this scenario?

3      

I came up with that I changed my TextField to a normal textfield without NumberFormatter. Changing the Int16 to Int could help here but I'm not sure with CoreData entities.

3      

In the end i just made it a String and made the keyboard numbers only. Major workaround but at least it lets me continue. Thanks all

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.