Objective-c KVC:用于多个属性的集合访问器模式,我如何使用它来增强我的代码? [英] Objective-c KVC: Collection Accessor Patterns for To-Many Properties, how can I use this to enhance my code?

查看:128
本文介绍了Objective-c KVC:用于多个属性的集合访问器模式,我如何使用它来增强我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读:集合访问器模式为多个属性,但我不知道我应该或应该在哪里使用这个。
有人可以指出一些情况,我可以使用集合访问器模式为多个属性更好的代码或使我的代码写作更容易吗? p>

I was reading :Collection Accessor Patterns for To-Many Properties, but I'm not sure where can I or should I use this. Can someone please point out some scenarios that I can use Collection Accessor Patterns for To-Many Properties to make my code better or make my code writing easier?

推荐答案

您正在阅读的集合访问器模式改善了键值编码与NSArray,NSDictionary等集合的工作方式。在你自己的类中实现它们,你的类可以像KVC一样使用标准的集合类。例如,也许你有一个 Flight 类,包含 Passenger 的列表。但也许这些乘客不是存储在一个NSArray,而是从数据库或东西。如果您实施以下方法:

The collection accessor patterns that you were reading about improve the way key value coding works with collections like NSArray, NSDictionary, etc. If you implement them in your own classes, your classes can be used with KVC just like the standard collection classes. For example, maybe you've got a Flight class that contains a list of Passenger. But maybe those passengers aren't stored in an NSArray, but pulled from an database or something. If you implement the following methods:


  • -countOfPassengers

  • -passengersAtIndexes:

  • -getPassengers:range:

  • -countOfPassengers
  • -passengersAtIndexes:
  • -getPassengers:range:

那么使用您的班级的代码可以使用KVC有效地访问乘客列表。例如,您可能会得到乘客常旅客号码列表(假设 Passenger 有这样的属性):

then code using your class can use KVC to efficiently access the passenger list. For example, you might get a list of passenger frequent flyer numbers (assuming Passenger has such a property) like this:

NSArray *frequentFlyerNumbers = someFlight.passengers.frequentFlyerNumbers;

这是很方便的东西 - 避免你不得不迭代收集和收集所有这些数字。如果你不知道你可以使用标准的集合像NSArray,这可能看起来很奇怪,所以需要花一分钟来吸收这一部分,然后考虑通过实现一些简单的方法,你做了同样的事情您自己的 Flight 课程。这很好,即使乘客的存储完全是你的班上的内部,这个工作是很好的。

That's pretty handy stuff -- saves you from having to iterate over the collection and collect all those numbers yourself. If you weren't aware that you could do that with the standard collections like NSArray, this might seems surprising, so take a minute to absorb that part first and then consider that by implementing a few simple methods you've done the same thing for your own Flight class. It's pretty cool that this works even though the storage of the passengers is entirely internal to your class.

现在,上面的方法只覆盖getter,但你可以做对于设置者:

Now, the methods above only cover getters, but you can do the same for setters by implementing:


  • -insertPassengers:atIndexes:

  • -removePassengersAtIndexes:

  • -replacePassengersAtIndexes:withPassengers:

  • -insertPassengers:atIndexes:
  • -removePassengersAtIndexes:
  • -replacePassengersAtIndexes:withPassengers:

现在,您的代码可以使用KVC添加和删除航班中的乘客:

Now, again, your code can add and remove passengers from a flight using KVC:

[someFlight replacePassengersAtIndexes:bumpedPassengerIndexes 
                        withPassengers:passengersFromLateConnectingFlight];

还有其他的KVC存取程式可以实现,但我不认为我需要列出他们所有为你,因为他们在那里你在链接的文章。关键的是,键值编码是在许多地方使用的有用的东西,通过实施一小部分方法,你可以使自己的类KVC兼容。

There are other KVC accessors that you can implement, too, but I don't think I need to list them all for you since they're right there in the article that you linked. The point is that key value coding is useful stuff that's used in a number of places, and by implementing a small handful of methods you can make your own classes KVC compliant.

这篇关于Objective-c KVC:用于多个属性的集合访问器模式,我如何使用它来增强我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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