Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- self.score = 5;
- self.score++
- -(void) createObject {
- Score *scoreEntity = (Score *)[NSEntityDescription
- insertNewObjectForEntityForName:@"Score"
- inManagedObjectContext:self.managedObjectContext];
- SpaceshipScene *spaceshipSceneReference = [[SpaceshipScene alloc] init];
- id points = [NSNumber numberWithInteger: spaceshipSceneReference.score];
- scoreEntity.points = points;
- scoreEntity.playerName = @"Joe";
- NSError *error = nil;
- // Saves the managedObjectContext
- if (! [[self managedObjectContext] save:&error] ) {
- NSLog(@"An error! %@", error);
- }
- }
- AppDelegate *appDelegateReference = [[AppDelegate alloc] init];
- [appDelegateReference createObject];
- -(void)fetchObject {
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
- NSEntityDescription *entity = [NSEntityDescription entityForName:@"Score"inManagedObjectContext:self.managedObjectContext];
- [fetchRequest setEntity:entity];
- // Sort fetched data
- NSSortDescriptor *sortByPoints = [[NSSortDescriptor alloc] initWithKey:@"points" ascending:NO];
- // Put them in an array
- NSArray *sortDescriptor = [[NSArray alloc] initWithObjects:sortByPoints, nil];
- // Pass the array to the fetch request
- [fetchRequest setSortDescriptors:sortDescriptor];
- NSError *error = nil;
- NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
- if (fetchedObjects == nil) {
- NSLog(@"Problem %@", error);
- }
- for (Score *s in fetchedObjects) {
- NSLog(@" %@ %d",s.playerName, [s.points integerValue]);
- }
- }
- AppDelegate *appDelegateReference = [[AppDelegate alloc] init];
- [appDelegateReference fetchObject];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement