James Callyway
James Callyway

Reputation: 55

How to get integer value from coredata to load to text

I am trying to get

[self.cellTextField setText:[self.person valueForKey:@"cellnumber"]];

to load the cell number.

It is an integer from a Core Data person entity. Right now it crashes and will only load a string, not an integer.

Upvotes: 0

Views: 679

Answers (2)

Jake Spencer
Jake Spencer

Reputation: 1127

Core Data stores numbers as NSNumbers, and NSNumber has a method called stringValue. Try this:

[self.cellTextField setText:[[self.person valueForKey:@"cellnumber"] stringValue]];

Upvotes: 3

Undo
Undo

Reputation: 25687

This is what I can tell without an error log to go off of:

You have a label in your UITableViewCell that you need to display the value of the cellnumber key in self.person.

Here I am assuming that you aren't making some basic mistake (like self.person being an array, etc), and that the value of the cellnumber key is an int.

Since the value of that key is an int, you can't just give it to something that expects a string. You'll need to convert it to an NSString first. Here's how:

NSString *personCellNumberString = [NSString stringWithFormat:@"%d", [self.person valueForKey:@"cellnumber"]];

[self.cellTextField setText:personCellNumberString];

Upvotes: 0

Related Questions