保存 iOS 应用程序数组

发布于 2024-11-14 21:13:50 字数 2710 浏览 5 评论 0原文

我想知道如何保存我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-11-21 21:13:50

您可能只是将应用程序发送到后台而不终止它们。在这种情况下,applicationWillTerminate:将不会被调用。这似乎是可能的原因,因为其余代码看起来不错。

而且您的 GetApplicationDocumentsDirectory 的命名似乎有点奇怪,因为它获取存储数组的文件的路径。除此之外,

NSString *fullFileName = [NSString stringWithFormat:@"%@/TarefasSalvas.plist", documentsDirectory];

这不是获取文件路径的正确方法。您应该像这样在 documentsDirectory 上使用 stringByAppendingPathComponent: 方法,

NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent:@"TarefasSalvas.plist"];

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,

NSString *fullFileName = [NSString stringWithFormat:@"%@/TarefasSalvas.plist", documentsDirectory];

is not the correct way of getting the file path. You should use thestringByAppendingPathComponent: method on documentsDirectory like this,

NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent:@"TarefasSalvas.plist"];

It would also make sense to declare fullFileName as the static variable rather than documentsDirectory.

好菇凉咱不稀罕他 2024-11-21 21:13:50

谢谢@Deepak。
在您向我提供有关在调试时发出日志警报的提示后,我注意到 iPad 正在终止该应用程序,然后该应用程序无法调用您之前提到的 applicationWillTerminate: 方法。
所以我解决了将代码更改为的问题:

- (void)applicationWillTerminate:(NSNotification *)notification{
    [self SavingStatus];
}

-(void)SavingStatus {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:lisTitulos];
    [array addObject:lisImagens];
    [array addObject:lisData];
    [array addObject:lisDetalhes];

    [array writeToFile:[self GetApplicationDocumentsDirectory] atomically:YES]; 
}

如您所见,我创建了一个在 applicationWillTerminate: 方法上调用的方法,并且每次应用程序重新加载表数据时:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

  ...

    [self SavingStatus];

    return cell;

}

通过这种方式,即使您的设备在正常关闭之前杀死应用程序,无论如何信息都会被保存=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:

- (void)applicationWillTerminate:(NSNotification *)notification{
    [self SavingStatus];
}

-(void)SavingStatus {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:lisTitulos];
    [array addObject:lisImagens];
    [array addObject:lisData];
    [array addObject:lisDetalhes];

    [array writeToFile:[self GetApplicationDocumentsDirectory] atomically:YES]; 
}

As you can see, I created a method that I call on applicationWillTerminate: method and everytime the app reload the Table data:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

  ...

    [self SavingStatus];

    return cell;

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文