Kosoku/Agamotto


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

License: MIT

Language: Objective-C

Keywords: ios, kvo, macos, tvos, watchos


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

Project Statistics

Sourcerank 6
Repository Size 142 KB
Stars 4
Forks 0
Watchers 2
Open issues 0
Dependencies 2
Contributors 1
Tags 20
Created
Last updated
Last pushed

Top Contributors See all

William Towe

Packages Referencing this Repo

Agamotto
Agamotto is an iOS/macOS/tvOS/watchOS framework that provides block based extensions to KVO and N...
Latest release 1.1.0 - Updated - 4 stars

Recent Tags See all

1.1.0 September 14, 2018
1.0.1 September 09, 2018
1.0.0 April 20, 2018
0.16.1 December 02, 2017
0.16.0 October 24, 2017
0.15.0 September 07, 2017
0.14.0 August 14, 2017
0.13.0 August 14, 2017
0.12.0 July 27, 2017
0.11.0 June 30, 2017
0.10.0 May 16, 2017
0.9.0 May 08, 2017
0.8.0 May 07, 2017
0.7.0 May 06, 2017
0.6.0 May 06, 2017

Something wrong with this page? Make a suggestion

Last synced: 2018-10-23 07:04:13 UTC

Login to resync this repository