欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios开发学习之kvc的使用解析

程序员文章站 2022-04-12 19:55:22
ios开发学习之k的使用解析 整个过程: 证明第一步,走set和_set方法: @interface person : nsobject @end #import &quo...

ios开发学习之k的使用解析

ios开发学习之kvc的使用解析

整个过程:

ios开发学习之kvc的使用解析

证明第一步,走set和_set方法:

@interface person : nsobject

@end

#import "person.h"

@implementation person

- (void)setage:(nsstring *)age
{
 nslog(@"_setage");
}

//- (void)_setage:(nsstring *)age
//{
  //  nslog(@"_setage");
//}

@end
 self.person = [[person alloc] init];

 nskeyvalueobservingoptions optips = nskeyvalueobservingoptionnew | nskeyvalueobservingoptionold;
 [self.person addobserver:self forkeypath:@"age" options:optips context:@"sdf"];

 [self.person setvalue:@10 forkey:@"age"];


- (void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context {
 nslog(@"监听到%@的%@属性发生了变化-%@ -%@",object, keypath, change, context);
}
结果:
当实现setage时 就会打印:
2018-07-18 23:18:23.984 newxc[5009:1640392] setage
当实现_setage时,就会打印
2018-07-18 23:18:23.984 newxc[5009:1640392] _setage
第二步:
不实现setage 和_setage


#import "person.h"

@implementation person

+ (bool)accessinstancevariablesdirectly
{
 return no;  // 不允许访问成员变量
}
结果:
会报错:
2018-07-18 23:20:35.584 newxc[5060:1644779] *** terminating app due to uncaught exception 'nsunknownkeyexception', reason: '[ valueforundefinedkey:]: this class is not key value coding-compliant for the key age.'
*** first throw call stack:
(
	0corefoundation 0x000000010aeceb0b __exceptionpreprocess + 171
	1libobjc.a.dylib0x000000010a933141 objc_exception_throw + 48
	2corefoundation 0x000000010aecea59 -[nsexception raise] + 9
	3foundation  0x000000010a511731 -[nsobject(nskeyvaluecoding) valueforundefinedkey:] + 226
	4foundation  0x000000010a440e1d -[nsobject(nskeyvaluecoding) valueforkey:] + 284
	5foundation  0x000000010a43bb44 nskeyvaluewillchangebysetting + 60
	6foundation  0x000000010a436057 nskeyvaluewillchange + 414
	7foundation  0x000000010a40c603 -[nsobject(nskeyvalueobservernotification) willchangevalueforkey:] + 477
	8foundation  0x000000010a51704b _nssetvalueandnotifyforundefinedkey + 45
	9foundation  0x000000010a448e8b -[nsobject(nskeyvaluecoding) setvalue:forkey:] + 292
	10  newxc 0x000000010a36316c -[viewcontroller viewdidload] + 476
	11  uikit 0x000000010b494cca -[uiviewcontroller loadviewifrequired] + 1235
	12  uikit 0x000000010b49510a -[uiviewcontroller view] + 27
	13  uikit 0x000000010b35d63a -[uiwindow addrootviewcontrollerviewifpossible] + 65
	14  uikit 0x000000010b35dd20 -[uiwindow _sethidden:forced:] + 294
	15  uikit 0x000000010b370b6e -[uiwindow makekeyandvisible] + 42
	16  uikit 0x000000010b2ea31f -[uiapplication _callinitializationdelegatesformainscene:transitioncontext:] + 4346
	17  uikit 0x000000010b2f0584 -[uiapplication _runwithmainscene:transitioncontext:completion:] + 1709
	18  uikit 0x000000010b2ed793 -[uiapplication workspacedidendtransaction:] + 182
	19  frontboardservices0x000000010e54b5f6 __fbsserialqueue_is_calling_out_to_a_block__ + 24
	20  frontboardservices0x000000010e54b46d -[fbsserialqueue _performnext] + 186
	21  frontboardservices0x000000010e54b7f6 -[fbsserialqueue _performnextfromrunloopsource] + 45
	22  corefoundation 0x000000010ae74c01 __cfrunloop_is_calling_out_to_a_source0_perform_function__ + 17
	23  corefoundation 0x000000010ae5a0cf __cfrunloopdosources0 + 527
	24  corefoundation 0x000000010ae595ff __cfrunlooprun + 911
	25  corefoundation 0x000000010ae59016 cfrunlooprunspecific + 406
	26  uikit 0x000000010b2ec02f -[uiapplication _run] + 468
	27  uikit 0x000000010b2f20d4 uiapplicationmain + 159
	28  newxc 0x000000010a36381f main + 111
	29  libdyld.dylib  0x000000010ddd665d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type nsexception
(lldb) 
但是 如果返回的是yes

+ (bool)accessinstancevariablesdirectly
{
 return yes; // 允许访问成员变量。
}
这里需要注意一下,这里依然会监听,可能是内部实现了kvo必须实现的那两个方法。
2018-07-18 23:22:18.703 newxc[5079:1647618] 监听到的age属性发生了变化-{
 kind = 1;
 new = 10;
 old = 0;
} -sdf

这个赋值的属性请看下面

ios开发学习之kvc的使用解析

ios开发学习之kvc的使用解析

ios开发学习之kvc的使用解析

ios开发学习之kvc的使用解析

ios开发学习之kvc的使用解析

ios开发学习之kvc的使用解析

上面是kvo实现逻辑的验证过程。

这个是forkey和forkeypath的解释:

#import 

@interface cat : nsobject

@property (nonatomic, strong) nsstring *wight;


@end


@interface person : nsobject

@property (nonatomic, strong) nsstring *age;

@property (nonatomic, strong) cat *cat;


@end
 [self.person setvalue:@10 forkey:@"age"]; // key只能访问简单的属性
 [self.person setvalue:@10 forkeypath:@"cat.wight"]; // 这个可以一层一层访问

1:通过kvc修改属性会触发kvo么?

会触发

原理:

2:kvc的赋值和取值过程是怎样的?原理是什么?