LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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 {
                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)
           = self.humName
                    newHum.location = self.humLoc
                    newHum.cigarcount = self.cigCnt

           = UUID()
                    do {
                        print("Humidor Created")
                    } catch {

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

struct NewHumidor_Previews: PreviewProvider {
    static var previews: some View {

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.




@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.


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.


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.


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'


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


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.


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


Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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.