gpt4 book ai didi

ios - 来自对象的一些数据要么不保存要么不加载

转载 作者:行者123 更新时间:2023-11-29 02:40:56 25 4
gpt4 key购买 nike

我正在开发一个简单的应用程序来跟踪我女儿的曲棍球比赛。我的问题是,当我停止并重新启动应用程序时,一些数据不会重新加载。(我正在检查一个只显示零的日志语句,不管之前有什么。)我不确定是否问题出在加载或保存上。

(很抱歉这篇文章很长,但我不知道该看哪里。)

它使用如下所示的 Games 类:

#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 属性被保存和加载,但其他属性都没有。

主 Controller 是一个 TableView Controller ,每场比赛都是一行。opponent 和 dateOfGame 属性在 TableView Controller 中设置,其他属性在选项卡栏 Controller 内的 View Controller 中设置。这些 Controller 的序列是通过第一个 Controller 的公开指示器和单击第二个 Controller 的行来完成的。使用以下代码,这些工作正常:

- (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);

}
}

我使用对手和 dateOfGame 的方法返回主 Controller ,并且我可以记录返回的数据。

-(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];
}


}

同样,我在一个设置数据值的长方法末尾使用这一行从另一个 Controller 传回其余数据,我也可以在此处记录返回的正确数据。

[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 数组中的某个地方存在差异,但我没有看到它。或者是其他问题。

谢谢。

最佳答案

问题出在我的游戏课上。我忘记包含使用

编码和解码 NSIntegers 的 key
[aCoder encodeInteger:self.shotsOnGoal forKey:@"ShotsOnGoal"];
self.shotsOnGoal = [aDecoder decodeIntegerForKey:@"ShotsOnGoal"];

等等

关于ios - 来自对象的一些数据要么不保存要么不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25814599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com