Last active
December 24, 2015 18:09
-
-
Save anddam/6841451 to your computer and use it in GitHub Desktop.
accessing ivars in a block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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