Reputation: 5107
in my iOS app I try to populate a core data store the first time the app is executed. I am using following code in my AppDelegate.m file, I will show as example how I am creating one object of the entity:
//creating new managedObject
NSManagedObject *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:[self managedObjectContext]];
//extracting year,month and day from current date
todayDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components
//converting components
NSNumber *yearBuscado = [NSNumber numberWithLong:[components year]];
NSNumber *mesBuscado = [NSNumber numberWithLong:[components month]];
NSNumber *diaBuscado = [NSNumber numberWithLong:[components day]];
//populating managedObject
[favoriteThing setValue:@"If done, swipe to right " forKey:@"thingName"];//String
[favoriteThing setValue:[NSNumber numberWithInt:10] forKey:@"displayOrder"];//Integer32
[favoriteThing setValue:[NSNumber numberWithInt:yearBuscado] forKey:@"todoYear"];//Integer32
[favoriteThing setValue:[NSNumber numberWithInt:mesBuscado] forKey:@"todoMonth"];//Integer32
[favoriteThing setValue:[NSNumber numberWithInt:diaBuscado] forKey:@"todoDay"];//Integer32
[favoriteThing setValue:@"Urgent" forKey:@"urgent"];//String
[favoriteThing setValue:@"Yellow" forKey:@"color"];//String
[favoriteThing setValue:@"Not deleted" forKey:@"isSemiDeleted"];//String
[favoriteThing setValue:@"Not done" forKey:@"isDone"];//String
This is what is stored after launching the app the first time (extracted from SQLite manager):
What I need is to store the expected values for today (2014-01-04):
todoYear = 2014
todoMonth = 1
todoDay = 4
These three attributes are from type Integer 32
What am I doing wrong?
Thank you for your time.
Upvotes: 0
Views: 78
Reputation: 1677
You are mistakenly calling numberWithInt
with a NSNumber object, instead call setValue directly with that NSNumber
todayDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components
//converting components
NSNumber *yearBuscado = [NSNumber numberWithInteger:[components year]];
NSNumber *mesBuscado = [NSNumber numberWithInteger:[components month]];
NSNumber *diaBuscado = [NSNumber numberWithInteger:[components day]];
[favoriteThing setValue:yearBuscado forKey:@"todoYear"];//Integer32
[favoriteThing setValue:mesBuscado forKey:@"todoMonth"];//Integer32
[favoriteThing setValue:diaBuscado forKey:@"todoDay"];//Integer32
Also, you can generate the Model Classes from CoreData Model. So that you can set values to a NSManagedObject
through Properties
instead of doing Key-Value Coding.
Like
FavoriteThing *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:[self managedObjectContext]];
[favoriteThing setBuscado: yearBuscado];
Upvotes: 1
Reputation: 3607
Things have become easy with NSNumber in Modern objective C. you can define your number like:
NSNumber *todoYear = @2014;
NSNumber *todoMonth = @1;
NSNumber *todoDay = @4;
Compiler will take care of everything. Happy Coding :)
Look at these links for more info.
NSNumber Literals
modern Objective C Basics
EDIT
Use This:
NSNumber *yearBuscado = @([components year]);
Upvotes: 1