gpt4 book ai didi

iphone - 尝试将数组数据保存/加载到 Plist

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

我一直在逐步学习 Objective C 和 iPhone SDK,我目前的项目涉及构建一个存储数值数据(体育数据)的应用程序。这主要是为了学习目的,因为有多个应用程序可以做同样的事情。无论如何,我遇到了一些障碍。我的意图是将玩家列表存储在一个表中,并允许用户添加其他玩家。

目前,我有一个按钮,当按下该按钮时,“Ninjas”将被添加到表格中。我还在表格中启用了删除功能。不幸的是,我似乎无法弄清楚如何从 plist 中保存和加载数据。我遵循了各种教程和指南,但我无法弄清楚发生了什么。我怀疑我是从一个空数组加载数据并添加到该数组,但涉及数据的数组是与 plist 分开的数组。不幸的是,除此之外我有点迷失了。

每当我切换 View 时,数组中的数据都会被删除。但是,我注意到如果我离开并返回,数据仍然存在,但如果我离开相当长的时间,离开并重新启动 iphone 等,则数据不会保留。即使对于我没有使用过的应用程序,这种情况似乎也会发生保存。这只是 iPhone 保留数据以防用户意外退出程序的功能吗?

希望我能切实地解释我的问题。 TL:DR 版本:我想将数据添加到数组,将其保存到 plist,并在数组出现在屏幕上时从 plist 重新加载数据。下面的代码试图实现这一点,但没有成功。

谢谢

#import "RootViewController.h"
#import "NewPlayer.h"
#import "OptionsMenu.h"

@implementation RootViewController
@synthesize createdPlayers;
@synthesize listOfPlayers;

-(NSString *) pathOfFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [paths objectAtIndex:0];
return [documentFolder stringByAppendingFormat:@"myfile.plist"];

}


-(void)applicationWillTerminate:(NSNotification*)notification{
NSMutableArray *array = [[NSMutableArray alloc]init];
[array writeToFile: [self pathOfFile] atomically:YES];
}


- (void)viewDidLoad
{

NSString *filePath = [self pathOfFile];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
listOfPlayers.array = [array objectAtIndex:0];
[array release];

}

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

[super viewDidLoad];
listOfPlayers = [[NSMutableArray alloc] init];

}


-(IBAction)AddButtonAction:(id)sender{
[listOfPlayers addObject:@"Ninjas"];
[createdPlayers reloadData];
}

-(IBAction)switchView:(id)sender{

OptionsMenu *second = [[OptionsMenu alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}

-(IBAction)newView:(id)sender{
NewPlayer *second = [[NewPlayer alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated: YES];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{

[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfPlayers count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];

cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = UITextAlignmentCenter;

NSString *cellValue = [listOfPlayers objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

NSLog(@"delete section: %d rol: %d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]);
[listOfPlayers removeObjectAtIndex:[indexPath indexAtPosition:1]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

else if (editingStyle == UITableViewCellEditingStyleInsert)
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}


-(void)tableView:(UITableView *)listOfPlayers moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
}

-(void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
[super viewDidUnload];

// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}



- (void)dealloc{
[createdPlayers release];
[listOfPlayers release];
[super dealloc];
}

@end


#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

IBOutlet UITableView* createdPlayers;
IBOutlet UIButton* superCat;

NSMutableArray *listOfPlayers;

}

@property(nonatomic, retain) IBOutlet NSObject *listOfPlayers;

-(NSString *) pathOfFile;
-(void)applicationWillTerminate:(NSNotification*)notification;

-(IBAction)AddButtonAction:(id)sender;

-(IBAction)switchView:(id)sender;

-(IBAction)newView:(id)sender;

@property (nonatomic, retain) UITableView* createdPlayers;


@end

12 月 20 日代码更新:

-(NSString *) pathOfFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [paths objectAtIndex:0];
return [documentFolder stringByAppendingFormat:@"myfile.plist"];

}


-(void)applicationWillTerminate:(NSNotification*)notification
{
[self.listOfPlayers writeToFile:[self pathOfFile] atomically:YES encoding:NSUTF8StringEncoding error:NULL];

}



- (void)viewDidLoad
{
self.listOfPlayers = [[NSMutableArray alloc] init];

NSString *filePath = [self pathOfFile];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
self.listOfPlayers = array;
[array release];

}

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

[super viewDidLoad];


}

最佳答案

我不知道 plist。但是你可以尝试使用 NSData

NSArray *array;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
//Write data
[data writeToFile:filePath atomically:YES];
//Read data
NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *oldData = [fileManager contentsAtPath:filePath];
NSArray *newArray = [NSKeyedUnarchiver unarchiveObjectWithData:oldData]

关于iphone - 尝试将数组数据保存/加载到 Plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8550102/

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