Skip to content

Instantly share code, notes, and snippets.

@anddam
Last active December 24, 2015 18:09
Show Gist options
  • Save anddam/6841451 to your computer and use it in GitHub Desktop.
Save anddam/6841451 to your computer and use it in GitHub Desktop.
accessing ivars in a block
#import <Foundation/Foundation.h>
#import "AFJSONRequestOperation.h"
@interface Data : NSObject {
NSURLRequest *request;
AFJSONRequestOperation *operation;
id receiver;
SEL action;
}
- (void)fetch;
- (Data *)initWithURL:(NSURL *)anURL forReceiver:(id)aReceiver andSelector:(SEL)aSelector;
@end
@implementation Data
@synthesize JSON;
- (Data *)initWithURL:(NSURL *)anURL forReceiver:(id)aReceiver andSelector:(SEL)aSelector
{
self = [super init];
if (self) {
selector = aSelector;
receiver = aReceiver;
// anURL used is [NSURL URLWithString:@"http://echo.jsontest.com/one/two"]
request = [NSURLRequest requestWithURL:anURL];
operation = nil;
}
return self;
}
- (Data *)init
{
return [self initWithURL:nil forReceiver:nil andSelector:nil];
}
- (void)fetch
{
void (^successHandler)(NSURLRequest *, NSHTTPURLResponse *, id) = ^(NSURLRequest *aRequest, NSHTTPURLResponse *aResponse, id returnedJSON)
{
NSLog(@"Data returned - %@", aResponse);
[receiver performSelector:selector withObject:JSON withObject:self];
JSON = returnedJSON;
operation = nil;
};
operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:successHandler
failure:nil];
[operation start];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment