KVO 阅读(Introduction)

KVO是一种机制,允许对象在其他对象的特定属性发生变化时接收通知,尤其适用于应用程序中Model和Controller层之间的通信。本文详细解释了KVO的工作原理,包括如何确保对象遵循KVO规范,如何注册和注销观察者,以及KVO如何简化通知机制。
KVO Introduction

Key-value observing is a mechanism that allows objects to be notified of changes to specified properties of other objects.

KVO 是一种机制:当其它对象的特定属性改变时会通知某个对象

At a Glance

It is particularly useful for communication between model and controller layers in an application.

KVO 在 Model 和 controller 通信很有用。

A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller.

controller 对象通常观察 model 的属性,view 对象通过 controller 观察 model 的属性。

In addition, however, a model object may observe other model objects (usually to determine when a dependent value changes) or even itself (again to determine when a dependent value changes).

model 也可以观察其他对象的属性,甚至也可以观察自己。

You can observe properties including simple attributes, to-one relationships, and to-many relationships. Observers of to-many relationships are informed of the type of change made—as well as which objects are involved in the change.

观察的属性包括:attribute,to-one relationships,to-many relationships。

To use KVO, first you must ensure that the observed object, the Account in this case, is KVO compliant. Typically, if your objects inherit from NSObject and you create properties in the usual way, your objects and their properties will automatically be KVO Compliant. It is also possible to implement compliance manually.

首先确定被观察者支持 KVO。通常,若对象继承自 NSObject,且通过常规的方式创建属性,那么你的对象和属性则自动支持 KVO。当然,也可以手动实现 KVO 支持。

Next, you must register your observer instance, the Person, with the observed instance, the Account.

其次,需要把观察者注册到被观察者的 keypath 上。

In order to receive change notifications from the Account, Person implements the observeValueForKeyPath:ofObject:change:context: method, required of all observers. The Account will send this message to the Person any time one of the registered key paths changes.

为了接收到通知,观察者需要实现 observeValueForKeyPath:ofObject:change:context
被观察者注册的 key paths 改变,观察者就会收到通知。

Finally, when it no longer wants notifications, and at the very least before it is deallocated, the Person instance must de-register by sending the message removeObserver:forKeyPath: to the Account.

最后,当不再需要通知时,需要至少在观察者 dealloc 之前通过 removeObserver:forKeyPath: 移除注册。

KVO’s primary benefit is that you don’t have to implement your own scheme to send notifications every time a property changes. Its well-defined infrastructure has framework-level support that makes it easy to adopt—typically you do not have to add any code to your project. In addition, the infrastructure is already full-featured, which makes it easy to support multiple observers for a single property, as well as dependent values.

KVO 最大的好处是:不用添加任何代码可以实现属性发生变化时发送通知给观察者,同时它也是一个系统框架级的基础结构,更容易被人接受。另外,KVO 功能强大,支持多个观察者注册一个属性,也支持属性值之间的依赖。

Registering Dependent Keys explains how to specify that the value of a key is dependent on the value of another key.

Registering Dependent Keys 这一节讲述了如何指定一个 key 的 value 依赖于另一个 key 的 value。

Unlike notifications that use NSNotificationCenter, there is no central object that provides change notification for all observers. Instead, notifications are sent directly to the observing objects when changes are made. NSObject provides this base implementation of key-value observing, and you should rarely need to override these methods.

KVO 不同于 NSNotificationCenter,不需要中间对象转发通知,而是通知直接发送给观察者对象。

Key-Value Observing Implementation Details describes how key-value observing is implemented.

Key-Value Observing Implementation Details 这一节描述了 KVO 是如何实现的。

基于51单片机,实现对直流电机的调速、测速以及正反转控制。项目包含完整的仿真文件、源程序、原理图和PCB设计文件,适合学习和实践51单片机在电机控制方面的应用。 功能特点 调速控制:通过按键调整PWM占空比,实现电机的速度调节。 测速功能:采用霍尔传感器非接触式测速,实时显示电机转速。 正反转控制:通过按键切换电机的正转和反转状态。 LCD显示:使用LCD1602液晶显示屏,显示当前的转速和PWM占空比。 硬件组成 主控制器:STC89C51/52单片机(与AT89S51/52、AT89C51/52通用)。 测速传感器:霍尔传感器,用于非接触式测速。 显示模块:LCD1602液晶显示屏,显示转速和占空比。 电机驱动:采用双H桥电路,控制电机的正反转和调速。 软件设计 编程语言:C语言。 开发环境:Keil uVision。 仿真工具:Proteus。 使用说明 液晶屏显示: 第一行显示电机转速(单位:转/分)。 第二行显示PWM占空比(0~100%)。 按键功能: 1键:加速键,短按占空比加1,长按连续加。 2键:减速键,短按占空比减1,长按连续减。 3键:反转切换键,按下后电机反转。 4键:正转切换键,按下后电机正转。 5键:开始暂停键,按一下开始,再按一下暂停。 注意事项 磁铁和霍尔元件的距离应保持在2mm左右,过近可能会在电机转动时碰到霍尔元件,过远则可能导致霍尔元件无法检测到磁铁。 资源文件 仿真文件:Proteus仿真文件,用于模拟电机控制系统的运行。 源程序:Keil uVision项目文件,包含完整的C语言源代码。 原理图:电路设计原理图,详细展示了各模块的连接方式。 PCB设计:PCB布局文件,可用于实际电路板的制作。
【四旋翼无人机】具备螺旋桨倾斜机构的全驱动四旋翼无人机:建模与控制研究(Matlab代码、Simulink仿真实现)内容概要:本文围绕具备螺旋桨倾斜机构的全驱动四旋翼无人机展开研究,重点进行了系统建模与控制策略的设计与仿真验证。通过引入螺旋桨倾斜机构,该无人机能够实现全向力矢量控制,从而具备更强的姿态调节能力和六自由度全驱动特性,克服传统四旋翼欠驱动限制。研究内容涵盖动力学建模、控制系统设计(如PID、MPC等)、Matlab/Simulink环境下的仿真验证,并可能涉及轨迹跟踪、抗干扰能力及稳定性分析,旨在提升无人机在复杂环境下的机动性与控制精度。; 适合人群:具备一定控制理论基础和Matlab/Simulink仿真能力的研究生、科研人员及从事无人机系统开发的工程师,尤其适合研究先进无人机控制算法的技术人员。; 使用场景及目标:①深入理解全驱动四旋翼无人机的动力学建模方法;②掌握基于Matlab/Simulink的无人机控制系统设计与仿真流程;③复现硕士论文级别的研究成果,为科研项目或学术论文提供技术支持与参考。; 阅读建议:建议结合提供的Matlab代码与Simulink模型进行实践操作,重点关注建模推导过程与控制器参数调优,同时可扩展研究不同控制算法的性能对比,以深化对全驱动系统控制机制的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值