Skip to content

Instantly share code, notes, and snippets.

View nevyn's full-sized avatar

Nevyn Bengtsson nevyn

View GitHub Profile
_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, mysocket, 0, dispatch_get_main_queue());
dispatch_source_set_event_handler(_source, ^{
// do something with mysocket, because it has changed (has read, write or error)
};
dispatch_source_set_cancel_handler(source, ^{
close(mysocket);
});
dispatch_resume(_source);
// sen för att stänga socket:
foo(function(value) {
bar(value, function(value2) {
console.log("hey we're done", value2);
});
});
// becomes
var value = foo().wait();
var value2 = bar(value).wait();
@nevyn
nevyn / async.m
Last active August 29, 2015 13:56
Why would you want a common abstraction for asynchrony? Well, compare these two. Might've gone a bit overboard, but the code was so easy with tasks I didn't realize what I was heading into when I was writing the callback hell version... Note: This would've been just as succinct using MAFuture or ReactiveCocoa or whatever. The important part is h…
// callback hell
- (void)fetch:(NSURL*)url saveWithCompletion:(void(^)())myCompletion errorHandler:(void(^)(NSError*))errorHandler
{
[downloader fetchURL:url completion:^(NSData *data) {
[parser parse:data completion:^(NSDictionary *parsed, NSError *err) {
if(parsed) {
[database save:completion^{
if(myCompletion)
myCompletion();
}];
@nevyn
nevyn / oops.c
Last active August 29, 2015 13:56 — forked from davepeck/oops.c
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
uint8_t *signature, UInt16 signatureLen)
{
SSLBuffer hashCtx;
OSStatus err = _SSLVerifySignedServerKeyExchangeWithHashCtx(ctx, isRsa, signedParams,
signature, signatureLen, &hashCtx);
SSLFreeBuffer(hashCtx);
return err;
}
#import <CoreGraphics/CGGeometry.h>
static inline CGVector TCVectorMultiply(CGVector vector, CGFloat m);
static inline CGVector TCVectorMinus(CGPoint p1, CGPoint p2)
{
return CGVectorMake(
p1.x - p2.x,
p1.y - p2.y
);
@nevyn
nevyn / UIApplication+GFInterceptEvents.m
Created March 23, 2014 19:57
Intercepting shake events
@implementation UIApplication (GFInterceptEvents)
+ (void)lookback_swizzleSendEvent
{
Method orig = class_getInstanceMethod([UIApplication class], @selector(sendEvent:));
Method repl = class_getInstanceMethod([UIApplication class], @selector(lookback_sendEvent:));
method_exchangeImplementations(orig, repl);
}
- (void)lookback_sendEvent:(UIEvent*)event
{
0.0370 enter Lookback Recording
0.0760 enter Status Bar Notification
0.0820 exit App Settings
0.7280 enter HostVC
0.7280 enter
0.7280 enter Data V C
0.7280 enter
0.7290 enter Data V C
0.7290 enter
0.7290 enter Data V C
@nevyn
nevyn / CONTRIBUTING.md
Created April 17, 2014 09:12
some cocoapods docs I wrote until I noticed that http://guides.cocoapods.org/contributing/dev-environment.html already exists

Working with the CocoaPods code base

In case you're unusued to working with Ruby projects or the used build tools, this section explains some work flows that will help you work with the CocoaPods code base.

CocoaPods uses Bundler to manage its dependencies. After checking out the CocoaPods repository, run rake bootstrap (which runs bundler install and git submodule update --init --recursive) to install everything you need to run and work on CocoaPods.

Working on CocoaPods dependencies or subprojects

CocoaPods is split into three: This main project which builds a complete tool, CocoaPodsCore which contains the data model and file parsers, and Xcodeproj which modifies and creates Xcode project and workspace files. For some changes, you are likely to need to make changes to either of those, in addition to the main project. You can tell Bundler that you want to use your local checkout of either of these projects instead of the system-wide installed version when you run the main project's "pod" tool

### Keybase proof
I hereby claim:
* I am nevyn on github.
* I am nevyn (https://keybase.io/nevyn) on keybase.
* I have a public key whose fingerprint is 4746 A6E5 B607 8617 C3FD 7F18 635F 66E7 2215 2A28
To claim this, I am signing this object:
@nevyn
nevyn / parentprocess.m
Created May 29, 2014 17:20
Prints the name of all the parent processes of this process.
#import <Foundation/Foundation.h>
#import <sys/sysctl.h>
#import <sys/proc_info.h>
#import <libproc.h>
// http://www.objectpark.net/en/parentpid.html
pid_t OPParentIDForProcessID(pid_t pid)
{
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);