MeganLee MeganLee - 1 year ago 78
Objective-C Question

Having issues when trying to convert string input to an integer value that can be set as an entities int32 attribute

I am still fairly new to Objective-C and iOS development
. I am able to make an app run fine with core data when all the attributes are strings only. My problem occurs when i have an entity (i made a test one to show as an example) which has an attribute that is set to be of type integer 16(though i have tried setting both integer 16 and integer 64 and get the exact same errors) but i cannot seem to understand how i am supposed to convert the string input from a user to a format which will be accepted as a value to be set. I keep getting the same error messages (implicit conversion of NSInteger to IDNullable is disallowed in ARC) & (incompatible integer to pointer conversion sending NSInteger(AKA "long" to parameter of type ID Nullable)
ex 1:
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newEntity = [NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:context];

int valueOne = [self.valueOneIn.text intValue];

[newEntity setValue:valueOne forKey:@"value1"]; //!!2 errors listed above

ex 2 (above ex edited):

NSInteger valueOne = [self.valueOneIn.text intValue];

[newEntity setValue:valueOne forKey:@"value1"]; //!!2 errors listed above

ex 3 (ex1, just edited):

NSInteger *valueOne = [self.valueOneIn.text intValue]; //!!error

[newEntity setValue:valueOne forKey:@"value1"]; //!!2 errors listed above

I have attached two photos showing simple examples of the errors that i am getting. I have spent the past couple days looking up videos, online courses and even reading some possible solutions on stack overflow, but none seem to remedy the situation (my examples above were made in my attempts to use the potential solutions i had found but, most cover using core data and string values or NSDate values). Any help or nudge in the right direction would really (i cannot stress this enough, i mean really) be appreciated[example of error when setting to NSIntegerexample of error with NSInteger

Answer Source

The setValue:forKey: method wants an object as the value. But int and NSInteger are primitive numeric types, not objects. That's why the first two examples don't work. The third one doesn't work because a pointer to NSInteger is still not an object.

Assuming that valueOneIn is a text field, you should do something like:

NSInteger valueOne = [self.valueOneIn.text integerValue];
[newEntity setValue:@(valueOne) forKey:@"value1"];

The @(valueOne) syntax tells the compiler to convert the NSInteger variable valueOne to an instance of NSNumber. That's a class designed to wrap numeric values when objects are required, so it's what you need for setValue:forKey:. Also, note that the code uses integerValue instead of intValue-- which is better because the compiler will use the correct integer size depending on the platform you're targeting.

It would be better to use custom NSManagedObject subclasses for your entities than to use NSManagedObject directly. One major advantage is that setValue:forKey: will accept any object as the value. Subclasses will tell the compiler what object types are acceptable, so that the compiler can verify that you're using the correct types.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download