如何使用NSCoding在应用程序中保存/加载数据? [英] How do I save/load data in my application using NSCoding?

查看:99
本文介绍了如何使用NSCoding在应用程序中保存/加载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我浏览了已经问过的许多与此类似的问题;但是,我仍然不太了解发生了什么,或者至少我不知道如何在应用程序中进行操作。

Yes, I've looked through the numerous questions similar to this that were asked already; however, I still did not quite understand what was going on or at least I couldn't figure out how to do it in my application.

所以基本上我的实践应用程序有一个主视图控制器,可显示两个文本标签。这些文本标签由包含大型数组的NSObject类填充。用户在他们要转到的数组中输入数字,数组中该索引处的字符串成为一个标签中的文本,而用户输入的数字成为另一标签中的文本。用户还可以通过按下按钮从数组中收藏字符串。发生这种情况时,该字符串及其索引号将添加到驻留在主视图控制器中的两个单独的可变数组中。然后,当用户想要查看其所有收藏夹时,他们可以向上滑动以(模态化)显示一个具有所有用户收藏夹字符串的uitable视图。这是通过使用导航控制器/ segue将数据(2个可变数组)从主视图控制器传递到uitableviewcontroller来实现的。此时,在uitableviewcontroller中有2个可变数组,其中包含来自主视图控制器的字符串和索引号。然后,使用委托/协议将该数据传递回主视图控制器,以在主视图控制器的标签中设置文本(用户在uitableview上点击的行中)。

So basically my "practice" app has a main view controller that displays two text labels. These text labels are populated by an NSObject class that holds a large array. The user inputs the number in the array they want to go to, and the string at that index in the array becomes the text in one label, while the user-input number becomes the text in the other label. The user can also "favorite" a string from the array by pressing a button. When this happens that string and it's index number are added to two separate mutable arrays that reside in the main view controller. Then, when the user wants to see all of their "favorites", they can swipe up to (modally) bring up a uitableview that has all of the user's "favorite strings". This is achieved by passing the data (2 mutable arrays) from the main view controller to the uitableviewcontroller using a navigation controller/segue. At this point, there are 2 mutable arrays in the uitableviewcontroller which contain the strings and index numbers from the main view controller. This data is then passed back to the main view controller using delegate/protocol to set the text (in the row that the user tapped on the uitableview) in the label on the main view controller.

构建应用程序没有问题;一切正常。但是我不知道如何保存数据,以便当用户退出应用程序并重新打开它时,他们仍然可以看到带有他们喜欢的所有字符串的表格视图。另外,我只是假设我应该使用NSCoding;但是,如果我需要使用Core Data或其他任何东西,请告诉我。如果有人可以帮助我,我真的会非常感激。请提供非常详细的解释,并提供示例代码。

I have no problem building the app; everything works perfectly. But I have no idea how to save the data so that when the user quits the application and reopens it they can still see the table view with all of the strings that they've favorited. Also, I'm just assuming that in my case, I should use NSCoding; if, however, I need to use Core Data or anything else, then let me know. If anyone can help me out, I'd really really appreciate it. Very detailed explanations please, and sample code if possible.

推荐答案

事实证明,我一直都在这样做。我使用NSUserDefaults保存数组(前提是它们不为空),然后将它们设置为NSUserDefaults(如果它们的键存在)。我唯一的问题是试图弄清楚在哪里保存和加载。最初,我是在viewWillAppear& viewWillDisappear,但在退后一步并真正了解NSUserDefaults的功能之后,我在viewWillDisappear& viewDidLoad。谢谢您的帮助。干杯。

It turns out I was doing it right the whole time. I used NSUserDefaults to save the arrays provided that they weren't empty, then I set the arrays to their NSUserDefaults if their key existed. My only problem was trying to figure out where to do the saving and loading. At first, I was doing it in viewWillAppear & viewWillDisappear, but after taking a step back and really understanding what NSUserDefaults did, I did it in viewWillDisappear & viewDidLoad. Thanks for all the help anyhow. Cheers.

这篇关于如何使用NSCoding在应用程序中保存/加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆