从启动到启动存储 NSStrings(或其他数据)的快速而肮脏的方法?

发布于 2024-09-01 23:10:11 字数 186 浏览 5 评论 0原文

我想允许我的用户存储自定义短语,以显示在可编辑的 UITableView 中。

存储这些字符串的方法是怎样的又快又脏呢?

我对 iPhone 开发还很陌生。我了解 Core Data,但不知道如何使用它。如果可能的话,我会为了这个特定的项目而远离这种情况。这里可以使用 PLIST 文件吗?

示例代码值得赞赏。

I want to allow my user to store custom phrases, to be displayed in an editable UITableView.

What's a quick and dirty to store these strings?

I'm fairly new at iPhone development. I know about Core Data, but not how to use it. I would stay away form that just for this particular project if possible. Are PLIST files a possibility here?

Sample code is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

风轻花落早 2024-09-08 23:10:14

您可能需要 NSDefaults;查看用户默认编程主题指南。

You probably want NSDefaults; check out the User Defaults Programming Topics guide.

最笨的告白 2024-09-08 23:10:13

使用 NSUserDefaults这个问题中有一些代码。

Use NSUserDefaults. There's some code over in this question.

无戏配角 2024-09-08 23:10:13

如果你想使用 plists,这是最快的方法(我认为)。还有其他方法可以使用 NSCoding 将数据编码到文件中,但您可能需要一个自定义数据模型类,这更适合保存更多不适合规定数据模型的随机事物。另外,正如已经指出的,NSUserDefaults 也是一个选项

要保存您的工作:

-(void)applicationWillTerminate:(NSNotification *)notification {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *filePath = [documentsDirectory stringByAppendingString:@"info.plist"];
   NSMutableArray *array = [[NSMutableArray alloc] init];
// Add the objects you want to save to the array here ex: [array addObject:]
   [array writeToFile:filePath atomically:YES];
   [array release];  }

并恢复您保存的文件:

- (void)viewDidLoad {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *filePath = [documentsDirectory stringByAppendingString:@"info.plist"];
   if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
       NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    // Set your vaiables here in the order that you saved them ex. var = [array objectAtIndex:]
       [array release];
   }
   UIApplication *app = [UIApplication sharedApplication];
   [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(applicationWillTerminate:) 
                                             name:UIApplicationWillTerminateNotification 
                                           object:app];

    [super viewDidLoad];}

基本上,当您的应用程序即将退出时,您可以创建您想要的对象的数组保存并将它们写入文件,当应用程序再次启动时,您可以将该文件读回到数组中,并根据保存它们的顺序分配变量。然后您进行设置,以便当您的应用程序发送其 UIApplicationWillTerminateNotification 时,它会执行您的代码以将(大概)修改后的变量保存回文件中。

If you want to use plists this is the quickest way (I think) to go. There are other ways to do it with NSCoding to encode your data to a file, but you would probably need a custom data model class, this is more suited to saving more random things that wouldn't fit into a prescribed data model. Also, as has been pointed out, NSUserDefaults is also an option

To save your work:

-(void)applicationWillTerminate:(NSNotification *)notification {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *filePath = [documentsDirectory stringByAppendingString:@"info.plist"];
   NSMutableArray *array = [[NSMutableArray alloc] init];
// Add the objects you want to save to the array here ex: [array addObject:]
   [array writeToFile:filePath atomically:YES];
   [array release];  }

And to recover your saved file:

- (void)viewDidLoad {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *filePath = [documentsDirectory stringByAppendingString:@"info.plist"];
   if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
       NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    // Set your vaiables here in the order that you saved them ex. var = [array objectAtIndex:]
       [array release];
   }
   UIApplication *app = [UIApplication sharedApplication];
   [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(applicationWillTerminate:) 
                                             name:UIApplicationWillTerminateNotification 
                                           object:app];

    [super viewDidLoad];}

Basically, when your application is about to quit you create and array of the objects you want to save and write them to the file, and when you application starts again you read that file back into an array and assign your variables based on the order in which you saved them. Then you set it up so when your application sends its UIApplicationWillTerminateNotification it executes your code to save your (presumably) modified variables back into a file.

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