iOS_研究(1)_KVO_使用类别(category)向一个已存在的类添加一个属性,是否能用kvo观察

一丶如题:

使用类别(category)向一个已存在的类添加一个属性,是否能用kvo观察

二丶实验

1.正常情况:

@interface ViewController ()

@property (nonatomic, copy) NSString *tmpStr;

@property (weak, nonatomic) IBOutlet UITextField *myTextField;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tmpStr = @"测试用";

    [self addObserver:self forKeyPath:@"tmpStr" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    Log(@"<接收到通知: object:%@ keyPath:%@ change:%@>", object, keyPath,change);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.tmpStr = self.myTextField.text;
    Log(@"---self.tmpStr-->%@<-----",self.tmpStr);
}
Paste_Image.png

2.category

Category.h /Category.m

@interface UIViewController (Test)

@property (nonatomic, copy) NSString *tmp2Str;

@end

#import "UIViewController+Test.h"

static const void *tmp2StrKey =&tmp2StrKey;

@implementation NSObject (Category)


- (NSString *)tmp2Str
{
    return objc_getAssociatedObject(self, tmp2StrKey);
}

- (void)setTmp2Str:(NSString *)tmp2Str
{
    objc_setAssociatedObject(self, tmp2StrKey, tmp2Str, OBJC_ASSOCIATION_COPY);
}

@end

使用

#import "UIViewController+Test.h"

@interface ViewController ()

@property (nonatomic, copy) NSString *tmpStr;

@property (weak, nonatomic) IBOutlet UITextField *myTextField;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tmpStr = @"测试用";
    self.tmp2Str = @"Runtime生成属性";
    [self addObserver:self forKeyPath:@"tmp2Str" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    Log(@"<接收到通知: object:%@ keyPath:%@ change:%@>", object, keyPath,change);

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.tmp2Str = self.myTextField.text;
    Log(@"---self.tmp2Str-->%@<-----",self.tmp2Str);
}
Paste_Image.png

三丶结论

可以;

了解下kvo知识

在ObjC中要实现KVO则必须实现NSKeyValueObServing协议, NSObject已经实现了该协议,因此几乎所有的ObjC对象都可以使用KVO

使用:直接看文档

You must perform the following steps to enable an object to receive key-value observing notifications for a KVO-compliant property:
1.注册addObserver:forKeyPath:options:context:
Register the observer with the observed object using the method addObserver:forKeyPath:options:context:.
2.实现observeValueForKeyPath:ofObject:change:context:
Implement observeValueForKeyPath:ofObject:change:context: inside the observer to accept change notification messages.
Unregister the observer using the method removeObserver:forKeyPath: when it no longer should receive messages. At a minimum, invoke this method before the observer is released from memory.

重要:不是所有的类的属性,都能实现kvo,必须符合一个 KVO-compliant
**Important:**
Not all classes are KVO-compliant for all properties. You can ensure your own classes are KVO-compliant by following the steps described in KVO Compliance. Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such.

一般,只要集成NSObject,命名符合gui’a

IT文库 » iOS_研究(1)_KVO_使用类别(category)向一个已存在的类添加一个属性,是否能用kvo观察
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址