保存到 NSUserDefaults 的文件在创建新文件后随时间消失
Files saved to NSUserDefaults disappear over time after a new file is created
概览:
我正在开发我的第一个应用程序。它收集用户位置信息以跟踪自行车骑行。我将内容保存到 .txt 文件(坐标信息和属性信息),然后用户可以将他们的数据导出 iOS 应用程序以供进一步分析。我已经成功添加了保存文本文件功能来存储信息。
正在使用的应用程序:
用户输入自定义标题,然后将该名称附加到列出所有记录的游乐设施的表格视图中。在我调用 NSUserDefaults 的游乐设施创建中,这很关键,如果不调用 NSUserDefaults,游乐设施名称不会附加到列出所有游乐设施的表视图中。
let saveAction = UIAlertAction(title: "Save", style: .Default,
handler: { (action:UIAlertAction) -> Void in
// Allow for text to be added and appended into the RideTableViewController
let textField = alert.textFields!.first
rideContent.append(textField!.text!)
// Update permanent storage after deleting a ride
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
现在,当用户查看过去的游乐设施时,他们可以 select 旧的游乐设施并加载信息,那里一切都很好!
问题:
不知何故过了一会儿,搭便车会得到overwritten/lost吗?在 iPhone 上进行测试时,我可以记录一次骑行,然后几个小时后我在 tableview 中查看过去的骑行并且骑行在那里, 但是 如果我记录一次新的骑行然后查看过去的游乐设施几个小时前记录的游乐设施不见了,但新的游乐设施还在。
我调用 NSUserDefaults 的唯一其他时间是在表视图中。我调用它的以下实例在 viewDidLoad 中:
override func viewDidLoad() {
super.viewDidLoad()
// Set up an if statement so that if there is no saved content it does not throw an error, if there is, populate the information with rideContent, if not leave the list as empty
if NSUserDefaults.standardUserDefaults().objectForKey("rideContent") != nil {
// Save content of rideContent as permanent storage so the ride is saved even when the app has been closed, this restores the old data
rideContent = NSUserDefaults.standardUserDefaults().objectForKey("rideContent") as! [String]
}
}
如果用户滑动删除文件,我会调用它:
// This method will be called when a user tries to delete an item in the table by swiping to the left
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// create an if statement to select the delete function after a user swipes to the left on a cell in the table
if editingStyle == UITableViewCellEditingStyle.Delete {
//Delete an item at the row index level
rideContent.removeAtIndex(indexPath.row)
// Update permanent storage after deleting a ride
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
// This is called so that when a file has been deleted, it will reload the view to reflect this change
savedRideTable.reloadData()
}
}
我想如果下面的代码看起来很正常,这是否意味着保存到用户文档并不能提供真正的文件永久存储?有没有更好的地方来存储这些文件?我希望我不是太含糊,我很难追查为什么应用程序会这样做,或者到底是什么触发了这种情况。感谢 stackExchange 的帮助!
这是 NSUserDefaults
的正常行为。你不是在保存一个 txt 文件,你是在 NSUserDefaults
中存储一个 String
,这是两个不同的东西,即使最终结果可能相似。
当你打电话时:
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
第二次,旧值被新值替换。
您有三个选择:
- 继续使用
NSUserDefaults
class 来存储这些字符串:您可以在 rideContent 键下保存一个包含所有骑行数据的字符串数组。
- 使用保存在
NSDocumentDirectory
里面的真实txt文件:Read and write data from text file
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/Introduction/Introduction.html
- 使用
CoreData
:
https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/CoreDataSnippets/Introduction/Introduction.html
https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial
概览: 我正在开发我的第一个应用程序。它收集用户位置信息以跟踪自行车骑行。我将内容保存到 .txt 文件(坐标信息和属性信息),然后用户可以将他们的数据导出 iOS 应用程序以供进一步分析。我已经成功添加了保存文本文件功能来存储信息。
正在使用的应用程序: 用户输入自定义标题,然后将该名称附加到列出所有记录的游乐设施的表格视图中。在我调用 NSUserDefaults 的游乐设施创建中,这很关键,如果不调用 NSUserDefaults,游乐设施名称不会附加到列出所有游乐设施的表视图中。
let saveAction = UIAlertAction(title: "Save", style: .Default,
handler: { (action:UIAlertAction) -> Void in
// Allow for text to be added and appended into the RideTableViewController
let textField = alert.textFields!.first
rideContent.append(textField!.text!)
// Update permanent storage after deleting a ride
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
现在,当用户查看过去的游乐设施时,他们可以 select 旧的游乐设施并加载信息,那里一切都很好!
问题: 不知何故过了一会儿,搭便车会得到overwritten/lost吗?在 iPhone 上进行测试时,我可以记录一次骑行,然后几个小时后我在 tableview 中查看过去的骑行并且骑行在那里, 但是 如果我记录一次新的骑行然后查看过去的游乐设施几个小时前记录的游乐设施不见了,但新的游乐设施还在。
我调用 NSUserDefaults 的唯一其他时间是在表视图中。我调用它的以下实例在 viewDidLoad 中:
override func viewDidLoad() {
super.viewDidLoad()
// Set up an if statement so that if there is no saved content it does not throw an error, if there is, populate the information with rideContent, if not leave the list as empty
if NSUserDefaults.standardUserDefaults().objectForKey("rideContent") != nil {
// Save content of rideContent as permanent storage so the ride is saved even when the app has been closed, this restores the old data
rideContent = NSUserDefaults.standardUserDefaults().objectForKey("rideContent") as! [String]
}
}
如果用户滑动删除文件,我会调用它:
// This method will be called when a user tries to delete an item in the table by swiping to the left
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// create an if statement to select the delete function after a user swipes to the left on a cell in the table
if editingStyle == UITableViewCellEditingStyle.Delete {
//Delete an item at the row index level
rideContent.removeAtIndex(indexPath.row)
// Update permanent storage after deleting a ride
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
// This is called so that when a file has been deleted, it will reload the view to reflect this change
savedRideTable.reloadData()
}
}
我想如果下面的代码看起来很正常,这是否意味着保存到用户文档并不能提供真正的文件永久存储?有没有更好的地方来存储这些文件?我希望我不是太含糊,我很难追查为什么应用程序会这样做,或者到底是什么触发了这种情况。感谢 stackExchange 的帮助!
这是 NSUserDefaults
的正常行为。你不是在保存一个 txt 文件,你是在 NSUserDefaults
中存储一个 String
,这是两个不同的东西,即使最终结果可能相似。
当你打电话时:
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
第二次,旧值被新值替换。
您有三个选择:
- 继续使用
NSUserDefaults
class 来存储这些字符串:您可以在 rideContent 键下保存一个包含所有骑行数据的字符串数组。 - 使用保存在
NSDocumentDirectory
里面的真实txt文件:Read and write data from text file https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/Introduction/Introduction.html - 使用
CoreData
: https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/CoreDataSnippets/Introduction/Introduction.html https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial