可可 – 什么是KVC搜索模式mutableArrayValueForKey?

可可 – 什么是KVC搜索模式mutableArrayValueForKey?,第1张

概述我试图理解Cocoa的键值编码(KVC)机制好一点。我已经阅读了苹果的 Key-Value Programming Guide,但仍然对某些KVC方法搜索密钥有些困惑。特别是mutableArrayValueForKey :. 下面我要解释一下我如何理解valueForKey:KVC“getter”工作。然后我会得到关于mutableArrayValueForKey的问题。 有七种不同的“吸气剂” 我试图理解Cocoa的键值编码(KVC)机制好一点。我已经阅读了苹果的 Key-Value Programming Guide,但仍然对某些KVC方法搜索密钥有些困惑。特别是mutableArrayValueForKey :.

下面我要解释一下我如何理解valueForKey:KVC“getter”工作。然后我会得到关于mutableArrayValueForKey的问题。

有七种不同的“吸气剂”KVC方法:

- (ID)valueForKey:(Nsstring *)key;- (ID)valueForKeyPath:(Nsstring *)keyPath;- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;- (NSMutableArray *)mutableArrayValueForKey:(Nsstring *)key;- (NSMutableArray *)mutableArrayValueForKeyPath:(Nsstring *)keyPath;- (NSMutableSet *)mutableSetValueForKey:(Nsstring *)key;- (NSMutableSet *)mutableSetValueForKeyPath:(Nsstring *)keyPath;

在属性(名为myKey)中搜索值时,Apple的文档会声明valueForKey:这样搜索:

>接收器中的-getMyKey,-myKey和-isMyKey(按顺序)
>如果没有找到,它会尝试这些有序的,多个getter(NSArray):

// required:- (NSUInteger)countOfMyKey;// Requires At Least One:- (ID)objectInMyKeyAtIndex:(NSUInteger)index;- (NSArray *)myKeyAtIndexes:(NSIndexSet *)indexes;// Optional (improves performance):- (voID)getMyKey:(KeyClass **)buffer range:(NSRange)inRange;

>接下来,它会尝试这些无序的,很多的getter(NSSet):

- (NSUInteger)countOfMyKey;- (NSEnumerator *)enumeratorOfMyKey;- (KeyClass *)memberOfMyKey:(KeyClass *)anObject;

>接下来,它尝试直接访问实例变量,假设是以accessInstanceVariablesDirectly返回,按以下顺序:_myKey,_isMyKey,myKey,isMyKey。
>最后,它放弃并调用接收类的 – (ID)valueForUndefinedKey:(Nsstring *)键方法。通常这里会出现错误。

我的问题是,mutableArrayValueForKey的搜索顺序模式是什么?

Apple’s docs state this:

Accessor Search Pattern for Ordered
Collections

The default search pattern for
mutableArrayValueForKey: is as
follows:

The receiver’s class is searched for a
pair of methods whose names match the
patterns -insertObject:inAtIndex:
and -removeObjectFromAtIndex:
(corresponding to the NSMutableArray
primitive methods
insertObject:atIndex: and
removeObjectAtIndex: respectively),or
methods matching the pattern
-insert:atIndexes: and -removeAtIndexes: (corresponding to the
NSMutableArrayinsertObjects:atIndexes:
and removeObjectsAtIndexes: methods).
If at least one insertion method and
at least one removal method are found
each NSMutableArray message sent to
the collection proxy object will
result in some combination of
-insertObject:inAtIndex:,-removeObjectFromAtIndex:,-insert:atIndexes:,and -removeAtIndexes: messages being sent to the original receiver of
mutableArrayValueForKey:.
…etc…

这对我来说是没有意义的,因为它正在讨论“设定者”的方法。 mutableArrayValueForKey:返回一个NSMutableArray。上面列出的所有方法返回voID,并用于编辑NSMutableArray,而不是获取它。例:

- (voID)insertMyKey:(KeyClass *)keyObject inMyKeyAtIndex:(NSUInteger)index;- (voID)removeObjectFromMyKeyAtIndex:(NSUInteger)index;

任何想法苹果想在他们的文档中说,还是这可能是一个错误?

我的理论是mutableArrayValueForKey:可能采用与valueForKey类似的路径:在搜索时检索KVC值。我只是不知道真正的路径。

感谢您提供的任何帮助! 总结

以上是内存溢出为你收集整理的可可 – 什么是KVC搜索模式mutableArrayValueForKey?全部内容,希望文章能够帮你解决可可 – 什么是KVC搜索模式mutableArrayValueForKey?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1036594.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存