iPhone核心数据 - NSNumber和obj_exception_throw的问题

时间:2011-01-29 15:11:52

标签: cocoa-touch debugging ios core-data

哦,我真的很喜欢这个。也许你新鲜的眼睛可以做一些魔术......

我的应用程序在viewDidLoad中显示此行时崩溃。

NSString *fieldName = [NSString stringWithFormat:@"position%d", [objectPosition intValue]];

我认为这可能是由于它上面的这条线:

    NSNumber *objectPosition  = [[object position] integerValue];

fieldName行没有明确的错误。

一般警告包括:

  • 初始化使得整数指针不带强制转换

  • 'NSManagedObject'可能无法响应'-position'

  • obj_exception_throw

我检查了[class object]的类,它说它实际上是一个NSCFNumber。 我在NSNumber做错了吗?我哪里错了?

谢谢

演变

这里列出了整个方法,这里也是整个文件https://gist.github.com/801879

的链接
- (void)viewDidLoad {
  // Format page
  scrollView.contentSize = CGSizeMake(200, 430);

  // Initialise PositionsView array
  positionViews = [[NSArray alloc] initWithObjects:position1,position2,position3,position4,position5,position6,position7,position8,position9,position10,position11,position12,position13,position14,position15,position16,position17,position18,nil];

  // Load managedObject
  //Fetch All players from PlayerPosition where game matches this game and store in objects
  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"PlayerPosition" inManagedObjectContext:managedObjectContext_];  
  [request setEntity:entityDescription];  
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"(game = %@)", self.game];   //WHERE game = self.game
  [request setPredicate:pred];
  NSError *error;
  objects = [managedObjectContext_ executeFetchRequest:request error:&error]; //execute fetch and store in objects


  NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>    Populate View with data from the managedObject..    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

  for (NSManagedObject *object in objects) {
    //loop through the objects in PlayerPosition for this game and update UI elements.
    Player   *objectPlayer    = [object valueForKey:@"player"];

    NSNumber *objectPosition  = [[object position] integerValue];
    NSLog(@"Object: %@", [object valueForKey:@"position"]);
    NSString *fieldName = [NSString stringWithFormat:@"position%d", [objectPosition intValue]];
    PlayerPositionView *theField = [self valueForKey:fieldName];
    [[theField textLabel] setText:objectPlayer.playerShortName];
    NSLog(@"object Position: %@", [objectPosition description]);

  }
  NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>    ..view Data Loaded    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

  [super viewDidLoad];
}

1 个答案:

答案 0 :(得分:0)

[[object position] integerValue]将返回NSInteger,而不是NSNumber *。由于返回值不是对象,因此您不能(也不必)向其发送-intValue消息。只需在NSInteger中直接使用+stringWithFormat:值。