2011-01-29 54 views
0

哦,我真的在這一個泡菜。也許你的新鮮眼睛可以做一些魔術..iPhone核心數據 - 問題與NSNumber和obj_exception_throw

我的應用程序崩潰時,它的viewDidLoad這一行。

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

我認爲這可能是由於此行是它上面:

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

上有字段名行沒有明確的錯誤。

一般警告包括:

  • 初始化時將整數指針不進行強制轉換

  • 'NSManagedObject' 可能不會 ' - 位'

  • obj_exception_throw

迴應

我檢查了班級[object position],它說它實際上是一個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]; 
} 

回答

0

[[object position] integerValue]將返回NSInteger,不是NSNumber *。由於返回值不是一個對象,因此不能(也不必)發送一條消息給-intValue。只需在+stringWithFormat:中直接使用NSInteger值即可。

+0

我改變了這一點。我認爲這有所幫助。我現在可以在我的應用程序中訪問此頁面,而不會崩潰,一旦我添加了一條記錄並退出並嘗試返回,就會發出與消息「無法訪問變量」字段名稱相同的消息「並崩潰」 – Evolve 2011-01-30 00:51:02