Agamotto 2.0.3

Agamotto 2.0.3

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Aug 2021

Maintained by William Towe.



Agamotto 2.0.3

  • By
  • William Towe

Agamotto

Carthage compatible Version Platform License

Agamotto is an iOS/macOS/tvOS/watchOS framework that provides block based extensions to KVO and NSNotificationCenter. It handles tearing down the observer upon deallocation. It is based on part of the ReactiveCocoa Objective-C framework.

Installation

You can install Agamotto using cocoapods, Carthage, or as a framework.

Usage

You must do the weakSelf/strongSelf dance for any blocks passed to the observing methods. Otherwise a retain cycle will be introduced.

It is not required in the notification observing example because self is not called within the block.

#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