2011-06-22 103 views
0

我有一個NSLog問題,它不會保持負載之間的結果相同。它非常令人沮喪,但如果有人可以擺脫一些真的很棒的光。我試圖調用數組從我的應用委託NSLog不返回一致的結果

 NSLog(@"Selected Team 1 : %@", [[[appDelegate teamRoster]objectAtIndex:1] class]); 
     NSLog(@"Selected Team 0 : %@", [[[appDelegate teamRoster]objectAtIndex:0] class]); 

     NSLog(@"Selected Team : %@", [[[appDelegate teamRoster]objectAtIndex:indexPath.row] class]); 
     NSLog(@"Selected Team : %@", [[appDelegate teamRoster]objectAtIndex:indexPath.row]); 

三次連續我有這些返回

2011-06-22 09:56:54.734 CoCoach[33182:207] Selected Team 1 : _NSIndexPathUniqueTreeNode 
2011-06-22 09:56:54.735 CoCoach[33182:207] Selected Team 0 : __NSCFSet 
2011-06-22 09:56:54.735 CoCoach[33182:207] Selected Team : __NSCFSet 
2011-06-22 09:56:54.736 CoCoach[33182:207] Selected Team : {(
)} 
2011-06-22 09:56:54.737 CoCoach[33182:207] Selected Team 1 : _NSIndexPathUniqueTreeNode 
2011-06-22 09:56:54.737 CoCoach[33182:207] Selected Team 0 : __NSCFSet 
2011-06-22 09:56:54.738 CoCoach[33182:207] Selected Team : _NSIndexPathUniqueTreeNode 
2011-06-22 09:56:54.738 CoCoach[33182:207] Selected Team : <_NSIndexPathUniqueTreeNode: 0x703e6c0> 
2011-06-22 09:58:30.082 CoCoach[33189:207] Selected Team 1 : __NSArrayM 
2011-06-22 09:58:30.083 CoCoach[33189:207] Selected Team 0 : NSCFString 
2011-06-22 09:58:30.083 CoCoach[33189:207] Selected Team : NSCFString 
2011-06-22 09:58:30.084 CoCoach[33189:207] Selected Team : Koch 
2011-06-22 09:58:30.084 CoCoach[33189:207] Selected Team 1 : __NSArrayM 
2011-06-22 09:58:30.085 CoCoach[33189:207] Selected Team 0 : NSCFString 
2011-06-22 09:58:30.085 CoCoach[33189:207] Selected Team : __NSArrayM 
2011-06-22 09:58:30.086 CoCoach[33189:207] Selected Team : (
) 
2011-06-22 09:59:17.825 CoCoach[33192:207] Selected Team 1 : _UITableViewReorderingSupport 
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team 0 : NSCFString 
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team : NSCFString 
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team : Smith 
2011-06-22 09:59:17.827 CoCoach[33192:207] Selected Team 1 : _UITableViewReorderingSupport 
2011-06-22 09:59:17.827 CoCoach[33192:207] Selected Team 0 : NSCFString 
2011-06-22 09:59:17.828 CoCoach[33192:207] Selected Team : _UITableViewReorderingSupport 
2011-06-22 09:59:17.828 CoCoach[33192:207] Selected Team : <_UITableViewReorderingSupport: 0x5b3cf30> 

此外,當我只是檢查看什麼數組我得到以下。

2011-06-22 09:58:30.078 CoCoach[33189:207] Team Roster Array: (
    "Koch", 
    "Smith" 
) 

這與預期完全相同。

什麼給?有任何想法嗎。謝謝。

更新的功能:添加陣列

- (void)fetchRecords:(NSString *)teamToFind{ 

    teamRoster = [[NSMutableArray alloc] init]; 

    NSManagedObjectContext *context = [self managedObjectContext]; 
    // Define our table/entity to use 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teams" inManagedObjectContext:context]; 

    // Setup the fetch request 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entity]; 

    // Define how we will sort the records 
    NSSortDescriptor *teams = [[NSSortDescriptor alloc] initWithKey:@"Team" ascending:NO]; 

    NSArray *sortDescriptors = [NSArray arrayWithObject:teams]; 


    [request setSortDescriptors:sortDescriptors]; 
    [teams release]; 

    // Fetch the records and handle an error 
    NSError *error; 
    NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy]; 


    if (!mutableFetchResults) { 
     // Handle the error. 
     // This is a serious error and should advise the user to restart the application 
    } 

    // Save our fetched data to an array 
    [self setStoredTeams:mutableFetchResults]; 
    NSLog(@"Number of teams, %i", [storedTeams count]); 

// NSMutableArray *temp = [[NSMutableArray alloc] init]; 

    for (Teams *diffTeams in storedTeams) { 
     NSLog(@"Name: %@", diffTeams.Team); 
     NSSet *rowers = [[NSSet alloc] initWithSet:diffTeams.Rowers]; 
     for (Rowers *roster in rowers){ 
      if ([diffTeams.Team isEqualToString:teamToFind]) { 
       NSString *fullName = [NSString stringWithFormat:@"%@ %@", roster.FName, roster.LName]; 
       NSLog(@"%@", fullName); 
       [teamRoster addObject:fullName]; 
       [fullName release]; 
       NSLog(@"Team Roster Array: %@", teamRoster);     
      } 
     } 
    } 
    if (![context save:&error]) { 
     //This is a serious error saying the record could not be saved. 
     //Advise the user to restart the application 
    } 

    [mutableFetchResults release]; 
    [request release]; 
} 
+2

你保留對象加入到'[的appDelegate teamRoster]'正確? ,看起來像指針曾經被設置爲一個釋放的對象,並重用其他對象的內存地址。請在您設置陣列的位置添加代碼。 – rckoenes

+0

好吧,我會回答你的問題,在我的appDelegate中,設置數組後,我需要說[teamRoster retain]嗎?離開之前? –

+0

取決於你如何創建陣列。如果你使用類似[NSMutableArray數組]的方法,那麼是的。如果你使用了alloc/init風格創建,那麼不(可能) –

回答

0

我被釋放的NSString,我投入的對象,但在for循環中被創建對象併發布了第二次

0

我想你想你的NSLog參數使用NSStringFromClass([[[appDelegate teamRoster]objectAtIndex:1] class])

相關問題