模型类 - 它们是什么意思? [英] Model classes -- what the heck do they really mean?

查看:136
本文介绍了模型类 - 它们是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不能在MVC中得到Model对象的挂起。我不知道为什么我们不能只处理数组和字典和数组的字典?

I really can't seem to get the hang of Model objects in MVC. I wonder why we can't just work with arrays and dictionaries and arrays OF dictionaries?

我知道它们代表我的其他类操作和处理的数据。但是,他们应该建造什么正确的方式?假设我有一个plist,我想在表视图中读取和显示。我可以直接加载到我的 viewDidLoad 方法中的数组属性,然后使用它,对吧?为什么我要使用一个Model类,我怎么去构建它呢?

I understand that they represent the 'data' that my other classes manipulate and work with. But what is the proper way they should be constructed? Suppose I've got a plist I want to read and display in a table view. I could just load it directly into an array property in my viewDidLoad method and then use it, right? Why would I want to use a Model class, and how would I go about constructing it?

此外,任何链接到资源/博客,解释这一点将非常感谢!

In addition, any links to resources/blogs out there that explain this point would be greatly appreciated!

推荐答案

你绝对可以使用字典和数组 - (或所有)您的数据模型。 Core Data的NSManagedObject非常像字典。但有时你希望你的模型对象不仅仅是存储数据 - 你可能还希望他们知道各种数据位之间如何相互关联,对数据进行操作等等。

You absolutely can work with dictionaries and arrays -- there's nothing wrong with using those as part of (or all of) your data model. Core Data's NSManagedObject is very much like a dictionary. But sometimes you want your model objects to do more than just store data -- you might also want them to know something about how the various bits of data relate to each other, operate on the data, and so forth.

考虑如何在数据模型中表示一个Person。可以是字典,对吧?使用像firstName,lastName,middleName,address,phone,email,dateOfBirth这样的键。如果你使用一个普通的老字典,是否可变,你只需为每个键存储适当的值,你就完成了。如果这真的是你需要的,你就完成了。

Consider how you'd represent a Person in your data model. Could be a dictionary, right? With keys like firstName, lastName, middleName, address, phone, email, dateOfBirth... If you use a plain old dictionary, mutable or not, you just store the appropriate value for each key and you're done. If that's really all you need, you're done.

另一方面,你可以从这些字段派生很多其他信息,而不需要存储额外的数据。如果创建自定义Person类,您可以给它的键,如fullName,age,initials,monogram和vCard。自定义类可以具有用于操作存储的信息以得到适当值的每个的方法。示例:

On the other hand, you can derive a lot of other information from those fields without needing to store extra data. If you create a custom Person class, you can give it keys like fullName, age, initials, monogram, and vCard. The custom class could have methods for each of those that manipulate the stored information to come up with the appropriate values. Example:

- (NSString*)fullName
{
    return [NSString stringWithFormat:@"%@ %@ %@", self.firstName, self.middleName, self.lastName];
}

这只是一个非常简单的例子。也许你的应用程序管理一组仓库的整个库存。在这种情况下,你可能需要代表零件,箱,调色板,位置,供应商,目的地,顺序,发票,叉车,操作员等的类。当有人从箱子中挑选零件时,你想要那个箱子知道剩余多少部分,如果数字小于某个阈值,则发送通知。该通知可能会触发一个事件,告诉叉车要获得另一个调色板并将其传送到仓位。一旦拾取,零件应该从仓转移到订单。当然,订单应该知道如何生成适当的发票。等等。所有这些逻辑都可以作为模型的一部分。

That's just a very simple example. Maybe your app manages the entire inventory of a group of warehouses. In that case, you'd probably want classes representing part, bin, palette, location, supplier, destination, order, invoice, forklift, operator, etc. When someone picks a part from a bin, you'd want that bin to know how many parts are left and send a notification if the number is smaller than some threshold. That notification might trigger an event that tells a forklift to go get another palette of that kind of part and deliver it to the bin. Once picked, the part should be transferred from the bin to an order. The order, of course, should know how to generate an appropriate invoice. And so on. All this logic can be part of the model.

这篇关于模型类 - 它们是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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