ios - 如何在 iOS 应用程序中保存文本数据?

标签 ios objective-c

使用我的应用程序,用户将阅读我的应用程序,这将产生以下格式的文本字符串:阅读、日期/时间、性别、年龄。本质上,我正在做的是获取这个字符串并将其存储在不同 Storyboard的 TableView 中。然后,每次关闭并重新打开应用程序时,保存的值都应该保留。保存字符串的 Action 应该通过按下保存按钮来触发。如何实现?示例代码表示赞赏。

最佳答案

最简单的解决方案是使用 NSUserDefaults:

   // Add a string & save permanently
   NSMutableArray *savedStrings = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"SavedStrings"]];
   NSString *sampleString = @"Reading, 4 Feb 2017, 12:00, male, 30 years"; 
   [savedStrings addObject:sampleString];
   [[NSUserDefaults standardUserDefaults] setObject:savedStrings forKey:@"SavedString"];
   [[NSUserDefaults standardUserDefaults] synchronize];

    // At launch, get saved strings - use to populate the tableview
    NSMutableArray *savedStrings = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"SavedStrings"]];

关于ios - 如何在 iOS 应用程序中保存文本数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42036046/

相关文章:

objective-c - 在委托(delegate)回调期间应该如何延长委托(delegate)对象的生命周期?

ios - 使用 ObjC 类包含在 .cpp 文件中时出错(unqualified-id @class)

objective-c - 如何在 Mac OSX 的 objective c 中获取和设置壁纸?

iphone - 访问 UITableView 中单元格的文本

ios - UITableView 多选、预选项

iphone - 检测 MathJax 何时在 UIWebView 中完成加载

ios - UICollectionView 水平滚动不滚动到最后一项

ios - 如何使对象(SKSprite 节点)在单击时消失

ios - 在 Swift 中刷新 NSFetchedResultController

ios - 解析取消订阅 channel iOS 不工作