Agamotto 2.0.3

Agamotto 2.0.3

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released上次发布2021年8月

William Towe维护。



Agamotto 2.0.3

  • 作者
  • William Towe

Agamotto

Carthage compatible Version Platform License

Agamotto 是一个 iOS/macOS/tvOS/watchOS 框架,它为 KVO 和 NSNotificationCenter 提供基于块的扩展。它会在销毁时清理观察者。该框架基于部分 ReactiveCocoa Objective-C 框架

安装

您可以使用 cocoapodsCarthage 或作为一个框架来安装 Agamotto

使用

您必须对任何传递给观察方法块的进行 weakSelf/strongSelf 舞蹈。否则可能会引入保留周期。

在通知观察示例中不需要这样做,因为代码块中没有调用 self

#import <Agamotto/Agamotto.h>

static NSNotificationName const kTextDidChangeNotification = @"kTextDidChangeNotification";

@interface MyObject : NSObject
@property (copy) NSString *text;

- (void)foo;
@end

@implementation MyObject

- (instancetype)init {
	if (!(self = [super init]))
		return nil;
	
	__weak __typeof__(self) weakSelf = self;
	[self KAG_addObserverForKeyPath:@"text" options:0 block:^(NSString *keyPath, id _Nullable value, NSDictionary<NSKeyValueChangeKey, id> *change){
		__strong __typeof__(weakSelf) strongSelf = weakSelf;
		
		[self foo];
	}];
	
	[self KAG_addObserverToNotificationCenter:nil notificationName:kTextDidChangeNotification object:self block:^(NSNotification *notification){
		NSLog(@"notification %@",notification);
	}];
	
	return self;
}

- (void)foo {
	NSLog(@"text %@",self.text);
}

- (void)setText:(NSString *)text {
	_text = [text copy];
	
	[[NSNotificationCenter defaultCenter] postNotificationName:kTextDidChangeNotification object:self];
}

@end