infernouk infernouk - 14 days ago 6
Swift Question

Correct method to save an Int into core data?

I have an entity with an Int16 attribute.

enter image description here

I want to save

var userRepsCount = Int()
which is set by the below stepper method:

@IBAction func userRepsStepper(_ sender: UIStepper) {
userExerciseRepsCounter.text = Int(sender.value).description
self.userRepsCount = Int(sender.value)
}


I want to add this to the attribute and am using
userExercise.reps = userRepsCount
to do so, however I get the error


Cannot assign value of type 'Int' to type 'Int16'


I was under the impression that Int16 could store int's such as this without issue? What am I missing here?

Answer

Try wrapping it with Int16(userRepsCount), and then when you pull it, wrap it with Int(value) before you process it