Skip to content

NSNotification shorthands #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ConciseKit.podspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Pod::Spec.new do |s|
s.name = 'ConciseKit'
s.version = '0.1.2'
s.version = '0.1.3'
s.license = 'MIT'
s.summary = 'A set of Objective-C additions and macros that lets you write code more quickly.'
s.homepage = 'http://github.com/petejkim/ConciseKit'
s.author = { 'Peter Jihoon Kim' => '[email protected]' }

s.source = { :git => 'http://github.com/petejkim/ConciseKit.git', :tag => 'v0.1.2' }
s.source = { :git => 'http://github.com/goodtohear/ConciseKit.git' }

s.source_files = 'src/**/*.{h,m}'

s.clean_paths = 'ConciseKitSpecs'
s.preserve_paths = 'ConciseKitSpecs'

s.frameworks = 'Foundation'
end
Expand Down
8 changes: 8 additions & 0 deletions ConciseKitSpecs/ConciseKitSpecs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0924B4D71562944D008176FF /* Listener.m in Sources */ = {isa = PBXBuildFile; fileRef = 0924B4D61562944D008176FF /* Listener.m */; };
CB6C70DD14E21B3A0016D5D7 /* NSSetConciseKitSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = CB6C70DC14E21B3A0016D5D7 /* NSSetConciseKitSpec.m */; };
E90DAB9D12A66B1D000A36AE /* Foo.m in Sources */ = {isa = PBXBuildFile; fileRef = E90DAB9C12A66B1D000A36AE /* Foo.m */; };
E90DADE612A6A07A000A36AE /* CKMocks.m in Sources */ = {isa = PBXBuildFile; fileRef = E90DADE512A6A07A000A36AE /* CKMocks.m */; };
Expand Down Expand Up @@ -49,6 +50,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0924B4D51562944D008176FF /* Listener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Listener.h; sourceTree = "<group>"; };
0924B4D61562944D008176FF /* Listener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Listener.m; sourceTree = "<group>"; };
0924B4D91562976A008176FF /* CKNotifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKNotifications.h; sourceTree = "<group>"; };
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
Expand Down Expand Up @@ -145,6 +149,8 @@
E90DAB9C12A66B1D000A36AE /* Foo.m */,
E90DADE412A6A07A000A36AE /* CKMocks.h */,
E90DADE512A6A07A000A36AE /* CKMocks.m */,
0924B4D51562944D008176FF /* Listener.h */,
0924B4D61562944D008176FF /* Listener.m */,
);
path = Fixtures;
sourceTree = "<group>";
Expand Down Expand Up @@ -182,6 +188,7 @@
children = (
E95368A51521CA5A00AA3B81 /* CKAdditions.h */,
E95368A61521CA5A00AA3B81 /* CKMacros.h */,
0924B4D91562976A008176FF /* CKNotifications.h */,
E95368A71521CA5A00AA3B81 /* CKSingleton.h */,
E95368A81521CA5A00AA3B81 /* ConciseKit.h */,
E95368A91521CA5A00AA3B81 /* ConciseKit.m */,
Expand Down Expand Up @@ -276,6 +283,7 @@
E95368B11521CA5A00AA3B81 /* NSArray+ConciseKit.m in Sources */,
E95368B21521CA5A00AA3B81 /* NSDictionary+ConciseKit.m in Sources */,
E95368B31521CA5A00AA3B81 /* NSString+ConciseKit.m in Sources */,
0924B4D71562944D008176FF /* Listener.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
26 changes: 26 additions & 0 deletions ConciseKitSpecs/Spec/ConciseKitSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "ConciseKit.h"
#import "Foo.h"
#import "CKMocks.h"
#import "Listener.h"

#if __has_feature(objc_arc)
#define ARCLESS(block)
Expand Down Expand Up @@ -189,5 +190,30 @@
NSString *foo = @"foo";
assertThat($safe(foo), equalTo(@"foo"));
});


describe(@"$listen", ^{
NSString * notificationName = @"foo";
__block Listener * listener;
beforeEach(^{
listener = [[Listener alloc] init];
});
it(@"supports plain $notify", ^{
assertThatBool(listener.notificationWasReceived, equalToBool(NO));
$listen(notificationName, listener, @selector(receiveNotification));
$notify(notificationName);
assertThatBool(listener.notificationWasReceived, equalToBool(YES));
});
it(@"supports $notifyWithObject:", ^{
NSObject * foo = [[NSObject alloc] init];
assertThatBool(listener.notificationWasReceived, equalToBool(NO));
assertThat(listener.objectReceivedViaNotification, equalTo(nil));
$listen(notificationName, listener, @selector(receiveNotification:));
$notifyWithObject(notificationName,foo);
assertThatBool(listener.notificationWasReceived, equalToBool(YES));
assertThat(listener.objectReceivedViaNotification,is(foo));
});
});

}
DESCRIBE_END
9 changes: 9 additions & 0 deletions ConciseKitSpecs/Spec/Fixtures/Listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <Foundation/Foundation.h>

@interface Listener : NSObject
@property (readwrite) BOOL notificationWasReceived;
@property (nonatomic, strong) NSObject * objectReceivedViaNotification;

-(void)receiveNotification;
-(void)receiveNotification:(NSNotification*)notification;
@end
12 changes: 12 additions & 0 deletions ConciseKitSpecs/Spec/Fixtures/Listener.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import "Listener.h"

@implementation Listener
@synthesize notificationWasReceived,objectReceivedViaNotification;
-(void)receiveNotification{
self.notificationWasReceived = YES;
}
-(void)receiveNotification:(NSNotification *)notification{
[self receiveNotification];
self.objectReceivedViaNotification = notification.object;
}
@end
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ Useful when writing tests for asynchronous tasks. Default timeout is 10 seconds,
### NSValue shorthands
$nonretained(), $pointer(), $point(), $range(), $rect(), $size()

### NSNotificationCenter shorthands
$listen(@"foo", self, @selector(handleFoo:)) => [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleFoo:) name:@"foo" object:nil]
$notify(@"foo") => [[NSNotificationCenter defaultCenter] postNotificationName:@"foo" object:nil]
$notifyWithObject(@"foo", bar) => [[NSNotificationCenter defaultCenter] postNotificationName:@"foo" object:bar]

## Additions

### NSArray
Expand Down Expand Up @@ -226,11 +231,13 @@ Useful when writing tests for asynchronous tasks. Default timeout is 10 seconds,
[string $insert:@"foo" at:1] => [string insertString:@"foo" atIndex:1] (+ returns self)
[string $set:@"foo"] => [string setString:@"foo"] (+ returns self)


## Contributors

* [nolanw](http://github.com/nolanw)
* [listrophy](https://github.com/listrophy)
* [gerry3](https://github.com/gerry3) @ [Inigral](https://github.com/inigral)
* [michaelforrest](https://github.com/michaelforrest) @ [Good To Hear](https://github.com/goodtohear)

## License

Expand Down
16 changes: 16 additions & 0 deletions src/CKNotifications.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>

static __inline__ void
$listen(NSString * eventName, NSObject * target, SEL action)
{
[[NSNotificationCenter defaultCenter] addObserver:target selector:action name:eventName object:nil];
}

static __inline__ void
$notify(NSString * eventName){
[[NSNotificationCenter defaultCenter] postNotificationName:eventName object:nil];
}
static __inline__ void
$notifyWithObject(NSString * eventName, __strong NSObject * object){
[[NSNotificationCenter defaultCenter] postNotificationName:eventName object:object];
}
1 change: 1 addition & 0 deletions src/ConciseKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "CKMacros.h"
#import "CKAdditions.h"
#import "CKSingleton.h"
#import "CKNotifications.h"

@interface ConciseKit : NSObject {}

Expand Down