保存 iOS 应用程序数组
我想知道如何保存我的 iOS 应用程序的数组。我使用了一种方法,但它只保存在我的iPod Touch上。我在 iPad 上测试过,没有成功。
我需要的是一个代码来在任何设备上保存我的应用程序数组...
任何人都可以帮助我吗?
这是代码:
myAppViewController.h:
- (NSString *)GetApplicationDocumentsDirectory;
- (void)applicationWillTerminate:(NSNotification *)notification;
myAppViewController.m:
-(NSString*) GetApplicationDocumentsDirectory {
static NSString* documentsDirectory = nil;
if (documentsDirectory == nil) {
documentsDirectory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES)
objectAtIndex:0] retain];
}
NSString *fullFileName = [NSString stringWithFormat:@"%@/TarefasSalvas.plist", documentsDirectory];
return fullFileName;
}
- (void)applicationWillTerminate:(NSNotification *)notification{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:lisTitulos];
[array addObject:lisImagens];
[array addObject:lisData];
[array addObject:lisDetalhes];
[array writeToFile:[self GetApplicationDocumentsDirectory] atomically:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [self GetApplicationDocumentsDirectory];
//if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
if (array != nil){
lisTitulos = [array objectAtIndex:0];
lisImagens = [array objectAtIndex:1];
lisData = [array objectAtIndex:2];
lisDetalhes = [array objectAtIndex:3];
}
else {
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:@"dd/MM/YYYY"];
NSString *dateNow = [dateFormat stringFromDate:date];
lisTitulos = [[NSMutableArray alloc] initWithObjects:@"Titulo",nil];
lisImagens = [[NSMutableArray alloc] initWithObjects:@"0.png",nil];
lisData = [[NSMutableArray alloc] initWithObjects:dateNow,nil];
lisDetalhes = [[NSMutableArray alloc] initWithObjects:@"",nil];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
}
谢谢
I would like to know how to save arrays of my iOS app. I used a method, but it only saved on my iPod Touch. I'd tested on iPad and it didn't work.
What I need is a code to save my app array on any device...
Anyone can help me?
Here is the code:
myAppViewController.h:
- (NSString *)GetApplicationDocumentsDirectory;
- (void)applicationWillTerminate:(NSNotification *)notification;
myAppViewController.m:
-(NSString*) GetApplicationDocumentsDirectory {
static NSString* documentsDirectory = nil;
if (documentsDirectory == nil) {
documentsDirectory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES)
objectAtIndex:0] retain];
}
NSString *fullFileName = [NSString stringWithFormat:@"%@/TarefasSalvas.plist", documentsDirectory];
return fullFileName;
}
- (void)applicationWillTerminate:(NSNotification *)notification{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:lisTitulos];
[array addObject:lisImagens];
[array addObject:lisData];
[array addObject:lisDetalhes];
[array writeToFile:[self GetApplicationDocumentsDirectory] atomically:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [self GetApplicationDocumentsDirectory];
//if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
if (array != nil){
lisTitulos = [array objectAtIndex:0];
lisImagens = [array objectAtIndex:1];
lisData = [array objectAtIndex:2];
lisDetalhes = [array objectAtIndex:3];
}
else {
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:@"dd/MM/YYYY"];
NSString *dateNow = [dateFormat stringFromDate:date];
lisTitulos = [[NSMutableArray alloc] initWithObjects:@"Titulo",nil];
lisImagens = [[NSMutableArray alloc] initWithObjects:@"0.png",nil];
lisData = [[NSMutableArray alloc] initWithObjects:dateNow,nil];
lisDetalhes = [[NSMutableArray alloc] initWithObjects:@"",nil];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能只是将应用程序发送到后台而不终止它们。在这种情况下,
applicationWillTerminate:
将不会被调用。这似乎是可能的原因,因为其余代码看起来不错。而且您的
GetApplicationDocumentsDirectory
的命名似乎有点奇怪,因为它获取存储数组的文件的路径。除此之外,这不是获取文件路径的正确方法。您应该像这样在
documentsDirectory
上使用stringByAppendingPathComponent:
方法,将
fullFileName
声明为静态变量而不是也是有意义的文档目录
。It is possible that you're just sending the app to the background and not terminating them. On such occasion,
applicationWillTerminate:
will not be called. That seems to be the likely cause as the rest of the code looks fine.And your
GetApplicationDocumentsDirectory
seems a bit strangely named as it gets the path to the file in which the array is stored. In addition to that,is not the correct way of getting the file path. You should use the
stringByAppendingPathComponent:
method ondocumentsDirectory
like this,It would also make sense to declare
fullFileName
as the static variable rather thandocumentsDirectory
.谢谢@Deepak。
在您向我提供有关在调试时发出日志警报的提示后,我注意到 iPad 正在终止该应用程序,然后该应用程序无法调用您之前提到的
applicationWillTerminate:
方法。所以我解决了将代码更改为的问题:
如您所见,我创建了一个在
applicationWillTerminate:
方法上调用的方法,并且每次应用程序重新加载表数据时:通过这种方式,即使您的设备在正常关闭之前杀死应用程序,无论如何信息都会被保存=D。
再次感谢@Deepak
以及所有回复我的人。
Thanks @Deepak.
After u giving me the hint about making the Log Alerts on Debugging, I noticed that the iPad is killing the App and then the app can't call the
applicationWillTerminate:
method as you mentioned before.So I solved the Problem changing my code to:
As you can see, I created a method that I call on
applicationWillTerminate:
method and everytime the app reload the Table data:By this way, even if your device Kill the App before it Close normally, the informations will be saved anyway =D.
Thanks again to @Deepak
and everybody who replied me.