使用 Core Data 在实体中插入和修改记录
我试图在互联网上找到我的问题的答案,但我找不到。 我在核心数据中有一个简单的实体,它有一个 Value 属性(即整数)和一个 Date 属性。我想在 .m 文件中定义两个方法。
第一种方法是ADD方法。它需要两个参数:一个整数值(由用户在 UI 中输入)和一个日期(默认为当前日期)。然后根据参数将记录插入到实体中。
第二种方法类似于增量方法。它使用日期作为键来查找记录,然后递增该记录的整数值。
我不知道如何编写这些方法。 (假设我们有一个用于 xib 文件中的表的数组控制器)
I tried to find the answer of my question on the internet, but I could not.
I have a simple entity in Core data that has a Value attribute (that is integer) and a Date attribute. I want to define two methods in my .m file.
First method is the ADD method. It takes two arguments: an integer value (entered by user in UI) and a date (current date by default). and then insert a record into the entity based on the arguments.
Second method is like an increment method. It uses the Date as a key to find a record and then increment the integer value of that record.
I don't know how to write these methods. (assume that we have an Array Controller for the table in the xib file)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的第二种方法不适合添加到实体本身。它需要位于实体上方的某个位置,最有可能在您的控制器对象中。
第一种方法如下:
这是相当直接的 KVC(键值编码),我强烈建议您阅读 Apple 关于该主题的文档。
对于您的其他方法,应该在控制器中,您需要执行提取来查找记录。
这也是非常简单的核心数据访问。我建议您阅读 Core Data 的工作原理。
顺便说一句,使用日期作为唯一值是一个非常糟糕的设计。
更新
您需要了解 Core Data 的工作原理。 Apple 有关于 Core Data 如何工作的详细文档,如果失败,您可以购买我的书。互联网上有大量有关如何使用 Core Data 的信息。
这是基本的 Objective-C。如果您问这个问题,您需要先退一步并学习该语言的基础知识。
控制器从未在 xib 文件中定义。它在 xib 文件中被引用。再次强调,在深入研究之前,您需要先回到 Objective-C 的工作原理并学习基础知识。
The second method you are looking for is not appropriate to add to the entity itself. It needs to be somewhere above the entities, most likely in your controller object.
The first method is as follows:
This is fairly straight KVC (Key-Value Coding) and I highly recommend that you read Apple's docs on the subject.
For your other method, that should be in a controller, you need to perform a fetch to find the record.
This is also very straightforward Core Data access. I recommend you read up on how Core Data works.
As an aside, using a date as a unique is a very bad design.
UPDATE
You need to read up on how Core Data works. Apple has great documentation on how Core Data works and if that fails you can buy my book. There is a TON of information about how to use Core Data on the internet.
This is basic Objective-C. If you are asking this question you need to step way back and learn the fundamentals of the language first.
The controller is never defined in the xib file. It is referenced in the xib file. Again, you need to go back to the beginning of how Objective-C works and learn the basics before you dive this deep.