使用NSMutableData iOS进行键值观察 [英] Key-Value Observing with NSMutableData iOS

查看:140
本文介绍了使用NSMutableData iOS进行键值观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型中,我有一个称为data的NSMutableData对象.我想从另一个对象观察它,并在添加或删除数据时收到通知.

In my model I have NSMutableData Object called data. I want to observe it from another object and get a notification when data is added or removed.

我了解这是一个多对多关系,因此我无法通过先向观察者添加此代码来使用普通的KVO

I understand that this is a to-many relationship, so I can't use the normal KVO by first adding the observer with this code

[object addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew context:NULL];

,然后实现observeValueForKeyPath:ofObject:change:context:方法以在发送通知时执行某些操作.

and then implement the observeValueForKeyPath:ofObject:change:context: method to do something when the notification is send.

我已阅读 Key-价值观察文档,我发现了有关它的其他文章. 使用KVO与NSNotificationCenter观察对可变数组的更改 观察NSMutableArray进行插入/移除它们都以NSMutableArray为例. NSMutableData也可以吗?我不明白那时候我要实现什么.

I have read the Key-Value Observing Documentation and I found other posts about it. Observing Changes to a mutable array using KVO vs. NSNotificationCenter & Observing an NSMutableArray for insertion/removal They all use NSMutableArray as example. Is it also possible for NSMutableData? I don't understand what I have to implement then.

有人可以告诉我为使NSMutableData对象正常工作而必须在哪个类中实现什么才能获得KVO?或者,如果不可能的话,另一种解决方案?

Can someone tell me what I exactly have to implement in which class to get KVO for a NSMutableData object working? Or if this is not possible, another solution?

非常感谢!

推荐答案

KVO有一个常见的误解:无法观察其他KVO兼容属性的内部状态.

There is a common misunderstanding with KVO: It is not possible to observe the inner state of an otherwise KVO compliant property.

NSData就是这种情况:从KVO角度来看,对象的data属性不会更改,而是NSData的状态更改.如果NSMutableData为其内容公开KVO兼容属性,则可以通过观察键路径(例如@"data.contents")来观察更改,但事实并非如此.

That's the case with the NSData: From the KVO point of view the data property of your object does not change, it's its state of the NSData that changes. If NSMutableData would expose KVO compliant properties for its contents you could observe changes by observing a key path (like @"data.contents") but that's not the case.

KVO仅适用于KVC兼容属性,这些属性也已记录为KVO兼容.大多数Cocoa框架类都不提供这种保证,并且观察对象及其关键路径是一个错误.

KVO only works for KVC compliant properties that are also documented to be KVO compliant. Most Cocoa framework classes don't give that guarantee and it's a bug to observe the objects and their key paths.

针对您的情况的解决方案不是将NSMutableData公开为公共属性,而是使用不可变数据并将诸如appendData:之类的方法添加到自定义类中.这将使您有机会使用这些方法对数据对象进行更改并手动发出KVO通知(使用willChangeValueForKey:@"data" ...执行更改... didChangeValueForKey:@"data").

A solution for your case would be not to expose an NSMutableData as a public property but instead use an immutable data and add methods like appendData: to your custom class. That would give you the opportunity to do the changes to the data object in these methods and emit KVO notifications manually (using willChangeValueForKey:@"data" ... do the change ... didChangeValueForKey:@"data").

这篇关于使用NSMutableData iOS进行键值观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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