Advertisement
Guest User

Untitled

a guest
Jul 9th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. self.score = 5;
  2.  
  3. self.score++
  4.  
  5. -(void) createObject {
  6.  
  7. Score *scoreEntity = (Score *)[NSEntityDescription
  8. insertNewObjectForEntityForName:@"Score"
  9. inManagedObjectContext:self.managedObjectContext];
  10.  
  11. SpaceshipScene *spaceshipSceneReference = [[SpaceshipScene alloc] init];
  12.  
  13.  
  14. id points = [NSNumber numberWithInteger: spaceshipSceneReference.score];
  15.  
  16. scoreEntity.points = points;
  17. scoreEntity.playerName = @"Joe";
  18.  
  19.  
  20. NSError *error = nil;
  21. // Saves the managedObjectContext
  22. if (! [[self managedObjectContext] save:&error] ) {
  23. NSLog(@"An error! %@", error);
  24. }
  25. }
  26.  
  27. AppDelegate *appDelegateReference = [[AppDelegate alloc] init];
  28. [appDelegateReference createObject];
  29.  
  30. -(void)fetchObject {
  31.  
  32. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  33. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Score"inManagedObjectContext:self.managedObjectContext];
  34. [fetchRequest setEntity:entity];
  35.  
  36. // Sort fetched data
  37. NSSortDescriptor *sortByPoints = [[NSSortDescriptor alloc] initWithKey:@"points" ascending:NO];
  38. // Put them in an array
  39. NSArray *sortDescriptor = [[NSArray alloc] initWithObjects:sortByPoints, nil];
  40.  
  41. // Pass the array to the fetch request
  42. [fetchRequest setSortDescriptors:sortDescriptor];
  43.  
  44.  
  45. NSError *error = nil;
  46. NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
  47. if (fetchedObjects == nil) {
  48. NSLog(@"Problem %@", error);
  49. }
  50.  
  51. for (Score *s in fetchedObjects) {
  52. NSLog(@" %@ %d",s.playerName, [s.points integerValue]);
  53. }
  54. }
  55.  
  56. AppDelegate *appDelegateReference = [[AppDelegate alloc] init];
  57. [appDelegateReference fetchObject];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement