Skip to content

Agamotto is an iOS/macOS/tvOS/watchOS framework that provides block based extensions to KVO and NSNotificationCenter.

License

Notifications You must be signed in to change notification settings

Kosoku/Agamotto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b76a584 · Aug 14, 2021

History

99 Commits
Jun 12, 2021
Apr 20, 2018
Aug 14, 2021
Jun 12, 2021
Jun 12, 2021
Jun 12, 2021
Jun 12, 2021
Mar 17, 2017
Nov 1, 2020
Dec 2, 2017
Jun 29, 2020
May 5, 2017
Aug 14, 2021
Aug 14, 2021
Aug 14, 2021
Aug 14, 2021
Sep 26, 2018

Repository files navigation

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

About

Agamotto is an iOS/macOS/tvOS/watchOS framework that provides block based extensions to KVO and NSNotificationCenter.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published