in my iOS App I am trying to do following: In a ViewController there are several TextFields and a Save button. When the user taps on the Save button, the text of yearTextField must be stored on a core data attribute (todoYear:int32). This is the code for my Save method:
- (IBAction)SaveButtonAction:(id)sender {
AppDelegate* appDelegate = [AppDelegate sharedAppDelegate];
NSManagedObjectContext* context = appDelegate.managedObjectContext;
NSManagedObject *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:context];
NSString *todoText = ToDoTextField.text;
NSNumber *todoYear = [yearTextField.text intValue];
NSNumber *todoMonth = [monthTextField.text intValue];
NSNumber *todoDay = [dayTextField.text intValue];
NSNumber *todoHour = [hourTextField.text intValue];
NSNumber *todoMinute = [minuteTextField.text intValue];
NSString *todoUrgent = urgentTextField.text;
NSString *todoColor = colorTextField.text;
[favoriteThing setValue:todoText forKey:@"thingName"];
[favoriteThing setValue:[NSNumber numberWithInt:0] forKey:@"displayOrder"];
[favoriteThing setValue:todoYear forKey:@"todoYear"];
[favoriteThing setValue:todoMonth forKey:@"todoMonth"];
[favoriteThing setValue:todoDay forKey:@"todoDay"];
[favoriteThing setValue:todoHour forKey:@"todoHour"];
[favoriteThing setValue:todoMinute forKey:@"todoMinute"];
[favoriteThing setValue:todoUrgent forKey:@"urgent"];
[favoriteThing setValue:todoColor forKey:@"color"];
NSError *error;
if(! [context save:&error])
{
NSLog(@"Whoopw,couldn't save:%@", [error localizedDescription]);
}
}
And this is the exception:
self AddToDoViewController * 0x16578680 0x16578680
favoriteThing FavoriteThing_FavoriteThing_ * 0x1650da90 0x1650da90
todoText __NSCFString * @"h" 0x1659f2b0
todoColor __NSCFString * @"Black" 0x16542070
todoUrgent __NSCFString * @"Not urgent" 0x165139e0
todoYear NSNumber * 0x7dd 0x000007dd
todoMinute NSNumber * 0x25 0x00000025
urgentTextField UITextField * 0x165bc2d0 0x165bc2d0
colorTextField UITextField * 0x165b2de0 0x165b2de0
minuteTextField UITextField * 0x165b8380 0x165b8380
The attributes for 'todoText', 'todoColor', 'todourgent' are right, the error appears when reading the value for the 'todoYear' attribute. I don't know how to put the 'todoYear' value in the right way to be accepted correctly. Thank you in advance.
Use NSNumber
's numberWithInt
method:
NSNumber *todoYear = [NSNumber numberWithInt: [yearTextField.text intValue]];
NSNumber *todoMonth = [NSNumber numberWithInt: [monthTextField.text intValue]];
NSNumber *todoDay = [NSNumber numberWithInt: [dayTextField.text intValue]];
NSNumber *todoHour = [NSNumber numberWithInt: [hourTextField.text intValue]];
NSNumber *todoMinute = [NSNumber numberWithInt: [minuteTextField.text intValue]];
Or combine this all into an NSDate
...
NSString *dateString = [NSString stringWithFormat: @"%@-%@-%@ %@:%@:00 +0000",
yearTextField.text, monthTextField.text, dayTextField.text,
hourTextField.text, minuteTextField.text];
NSDate *todoDate = [NSDate dateWithString:dateString];
User contributions licensed under CC BY-SA 3.0