2014-09-12 4 views
0

간단한 딸랑이 게임을 추적하기 위해 노력하고 있습니다. 내 문제는 내가 중지하고 응용 프로그램을 다시 시작하면 일부 데이터가 다시로드되지 않습니다. (이전에 있었던 것에 관계없이 0을 나타내는 로그 문으로 확인합니다.) 확실하지 않습니다. 문제는로드 또는 저장 중입니다.개체의 일부 데이터가 저장되지 않거나로드되지 않음

#import <Foundation/Foundation.h> 

@interface Game : NSObject <NSCoding>//conforms to this protocol so data can be prepared for read/write to file 

//used primarily in GameFactsViewController 
@property (nonatomic, strong) NSString *opponent; 
@property (nonatomic, strong) NSDate *dateOfGame; 

//used primarily in PlayerActionsViewController 
@property (nonatomic) NSInteger shotsOnGoal; 
@property (nonatomic) NSInteger shotsNotOnGoal; 
@property (nonatomic) NSInteger passesCompleted; 
@property (nonatomic) NSInteger passesNotCompleted; 
@property (nonatomic) NSInteger takeaways; 
@property (nonatomic) NSInteger giveaways; 
@property (nonatomic) NSInteger faceoffsWon; 
@property (nonatomic) NSInteger faceoffsLost; 
@property (nonatomic) NSInteger shifts; 
@property (nonatomic) NSInteger blockedShots; 

@end 

내 문제입니다 상대와 :

(. 긴 게시물에 대한 미안하지만, 내가 볼 어디서 확실하지 않다)

그것은 다음과 같이 보이는 게임 클래스를 사용하여 dateOfGame 속성을 가져 와서 앱을 다시 시작할 때 저장되고로드되지만 다른 속성은 없습니다.

Tha 주 컨트롤러는 각 게임을 행으로 사용하는 tableview 컨트롤러입니다. 상대방 및 dateOfGame 속성은 테이블 뷰 컨트롤러에 설정되고 다른 컨트롤러는 탭 컨트롤러 내에있는보기 컨트롤러에 설정됩니다. 이 콘트롤러의 스팽글은 첫 번째에 대한 공개 표시기와 두 번째 행에 대한 클릭으로 만들어집니다. 내가 상대와 dateOfGame이 방법으로 메인 컨트롤러에 반환하고

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    //check for proper seque 
    if ([segue.identifier isEqualToString:@"AddGame"]) { 

     //identify the top level view controller holding the GameFactsVC (see storyboard) 
     UINavigationController *navigationController = segue.destinationViewController; 

     //go down one level to get the GFVC 
     GameFactsViewController *controller = (GameFactsViewController *) navigationController.topViewController; 

     //set the current controller (the HSVC) as the delegate for the GFVC 
     controller.delegate = self; 

    } else if ([segue.identifier isEqualToString:@"EditGame"]) { 

     //as above to get to the right place 
     UINavigationController *navigationController = segue.destinationViewController; 
     GameFactsViewController *controller = (GameFactsViewController *) navigationController.topViewController; 
     controller.delegate = self; 

     //"sender" here is what was clicked, the detail disclosure icon 
     //this identifies what game data to load to edit 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
     controller.gameToEdit = _games[indexPath.row]; 

    } else if ([segue.identifier isEqualToString:@"GameDetails"]) { 

     UITabBarController *tabBarController = segue.destinationViewController; 
     PlayerActionsViewController *controller = (PlayerActionsViewController *)[[tabBarController viewControllers] objectAtIndex:0]; 
     controller.delegate = self; 

     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 

     controller.gameToEditPerformance = _games[indexPath.row]; 
     NSLog(@"Data passed %ld", (long)controller.gameToEditPerformance.shotsOnGoal); 

    } 
} 

나는 돌아 오는 데이터를 기록 할 수 있습니다 : 다음은이 코드를 사용하여, 잘 작동.

-(IBAction) done 
{ 
    //Use delegate to capture entered data and pass to HSVC 
    //methods here are delegate methods listed above and defined in the HSVC 

    //see if the data was empty when view loaded 
    if (self.gameToEdit == nil) { 

     Game *game = [[Game alloc] init]; 
     game.opponent = self.opponentField.text; 
     game.dateOfGame = [self convertToDate:self.dateLabel.text withFormat:@"MMM d, yyyy"]; 


     [self.delegate gameFactsViewController:self didFinishAddingGame:game]; 

    } else { 
     self.gameToEdit.opponent = self.opponentField.text;//updates data model; will update display in delegate method 
     self.gameToEdit.dateOfGame = [self convertToDate:self.dateLabel.text withFormat:@"MMM d, yyyy"]; 

     [self.delegate gameFactsViewController: self didFinishEditingGame:self.gameToEdit]; 
    } 


} 

마찬가지로, 나는 데이터 값을 설정하는 긴 방법의 끝에이 줄을 다시 다른 컨트롤러에서 데이터의 나머지 부분을 통과하고 있고 나도 여기에 돌아 오는 정확한 데이터를 기록 할 수 있습니다.

[self.delegate playerActionsViewController:self didEditGameData:self.gameToEditPerformance]; 

나는 두 가지 유사한 방법으로 Save 메서드를 호출합니다. (난 아직도 기본보기를 업데이트 할 필요가있는 동안 나는 이미 다른 테이블의 표시를 업데이트 한 나는 그것을보고로서 주요 차이점.) 데이터 저장 및 로딩을위한 지금

- (void) gameFactsViewController:(GameFactsViewController *)controller didFinishEditingGame:(Game *)game 
{ 
    //edit already made in data model in GFVC 
    //need to update display 

    NSInteger index = [_games indexOfObject: game];//locate the item being edited in the games array 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];//get the right cell 

    [self configureDataForCell:cell withGame:game];//puts the game data into the labels in the cell 

    [self saveGames];//write the current data to the file 

    [self dismissViewControllerAnimated:YES completion:nil]; 

} 

- (void) playerActionsViewController: (PlayerActionsViewController *) controller didEditGameData: (Game *) game 
{ 
    NSLog(@"Paased back shots %ld", (long) game.shotsOnGoal); 
    [self saveGames]; 


} 

. 두 상황 사이에, 내 생각,

- (void) loadGames 
{ 
    NSString *path = [self dataFilePath];//for convenience below 

    //if there is already a data file, unarchive/decode and load games array 
    //else create an empty arry to hold games 
    if ([[NSFileManager defaultManager] fileExistsAtPath: path]) { 

     NSData *data = [[NSData alloc] initWithContentsOfFile: path];//data structure created and loaded with file data 
     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];//archiver created and connected to data 

     _games = [unarchiver decodeObjectForKey:@"Games"]; 

     [unarchiver finishDecoding];//data now in games array 

    } else { 

     _games = [[NSMutableArray alloc] initWithCapacity:50]; 
    } 
} 

- (void) saveGames 
{ 
    NSMutableData *data = [[NSMutableData alloc] init];//data structure to hold the data to be saved after encoding 
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 

    [archiver encodeObject:_games forKey:@"Games"];//I believe key here needs to match class name that will be saved. It tells archiver how to encode object properly 

    [archiver finishEncoding];//finish encoding, with data now in data structure 

    [data writeToFile:[self dataFilePath] atomically:YES];//write data structure to file determined above 

} 

어딘가에 여기 _games 배열에 차이가 있습니다,하지만 난 그것을 보이지 않아요 :이 코드를 사용하고 있습니다. 아니면 다른 문제입니다.

감사합니다.

+1

난 당신이 7 개 단어를 명중했습니다 확신 @Wain 당신의'NSCoding' 방법 – Wain

+0

에 대한 코드를보기 등을 인코딩하고

[aCoder encodeInteger:self.shotsOnGoal forKey:@"ShotsOnGoal"]; self.shotsOnGoal = [aDecoder decodeIntegerForKey:@"ShotsOnGoal"]; 

사용하여 NSIntegers를 해독하기 위해 키를 포함하는 것을 잊었다 . 나는이 키들을 정의하지 않았다. 하지만 시도 할 때 NSInteger를 인코딩 할 수 없습니다. 그 사람들은 NSNumber인가 아니면 다른 것입니까? – tangobango

답변

1

내 게임 클래스에 문제가 있습니다. 나는

관련 문제