From 348909fc044f3ee2ab90aa52c9ba36b90f3f49a1 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Wed, 14 Nov 2012 22:06:49 -0800 Subject: [PATCH] use ARC, remove retain/release/autorelease --- Marshmallows/MMHTTPClient.m | 27 +++++++++++------------ Marshmallows/MMHTTPRequest.h | 4 ++-- Marshmallows/MMHTTPRequest.m | 42 ++++++++++++++++-------------------- 3 files changed, 33 insertions(+), 40 deletions(-) diff --git a/Marshmallows/MMHTTPClient.m b/Marshmallows/MMHTTPClient.m index d827a52..8bcfcda 100644 --- a/Marshmallows/MMHTTPClient.m +++ b/Marshmallows/MMHTTPClient.m @@ -9,7 +9,6 @@ #import "MMHTTPClient.h" #import "NSString+marshmallows.h" -MMHTTPClient *_client; // Encode a string to embed in an URL. NSString* MMHTTPURLEncode(NSString *string) { return (__bridge NSString *) @@ -20,6 +19,7 @@ NSString* MMHTTPURLEncode(NSString *string) { kCFStringEncodingUTF8); } +MMHTTPClient *_sharedMMHTTPClient; NSString *JoinURLComponents(NSString *first, va_list args) { @@ -39,25 +39,25 @@ NSString *JoinURLComponents(NSString *first, va_list args) + (MMHTTPClient *) sharedClient { - if (!_client) { - _client = [[self alloc] init]; + if (!_sharedMMHTTPClient) { + _sharedMMHTTPClient = [[self alloc] init]; } - return _client; + return _sharedMMHTTPClient; } + (id) client { - return [[[self alloc] init] autorelease]; + return [[self alloc] init]; } + (id) clientWithBaseURL: (NSString *)baseURL { - return [[[self alloc] initWithBaseURL: baseURL] autorelease]; + return [[self alloc] initWithBaseURL: baseURL]; } + (id) clientWithBaseURL: (NSString *)baseURL timeout: (NSUInteger)timeout { - return [[[self alloc] initWithBaseURL: baseURL timeout: timeout] autorelease]; + return [[self alloc] initWithBaseURL: baseURL timeout: timeout]; } + (NSString *) pathFor: (NSString *)first, ... @@ -173,7 +173,11 @@ NSString *JoinURLComponents(NSString *first, va_list args) - (MMHTTPRequest *) getImage: (NSString *)url then: (MMHTTPImageCallback)callback { - return [self request: [NSDictionary dictionary] then: (MMHTTPCallback)callback]; + NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: + url, @"url", + @"image", @"type", + nil]; + return [self request: options then: (MMHTTPCallback)callback]; } - (MMHTTPRequest *) getText: (NSString *)url then: (MMHTTPTextCallback)callback @@ -203,7 +207,6 @@ NSString *JoinURLComponents(NSString *first, va_list args) return [self request: options then: callback]; } -- (MMHTTPRequest *) post: (NSString *)url fields: (NSDictionary *)fields then: (MMHTTPCallback)callback - (NSString *) encodeFields: (NSDictionary *)fields withPrefix: (NSString *)prefix { NSString *suffix = @""; @@ -283,10 +286,4 @@ NSString *JoinURLComponents(NSString *first, va_list args) return [MMHTTPRequest requestWithOptions: options callback: callback]; } -- (void) dealloc -{ - [_baseURL release]; - [super dealloc]; -} - @end diff --git a/Marshmallows/MMHTTPRequest.h b/Marshmallows/MMHTTPRequest.h index e4de3e9..eb9e64d 100644 --- a/Marshmallows/MMHTTPRequest.h +++ b/Marshmallows/MMHTTPRequest.h @@ -28,10 +28,10 @@ typedef void (^MMHTTPImageCallback)(NSInteger status, UIImage *image); @property (nonatomic, retain) NSMutableDictionary *headers; @property (nonatomic, retain) NSData *data; @property (nonatomic, retain) NSString *type; -@property (nonatomic, retain) MMHTTPCallback callback; +@property (nonatomic, copy) MMHTTPCallback callback; @property NSUInteger timeout; @property (readonly) NSInteger statusCode; -@property (readonly) NSDictionary *responseHeaders; +@property (strong, readonly) NSDictionary *responseHeaders; @property (readonly) NSData *responseData; @property (readonly) NSString *responseText; @property (readonly) UIImage *responseImage; diff --git a/Marshmallows/MMHTTPRequest.m b/Marshmallows/MMHTTPRequest.m index 27686b6..451ed36 100644 --- a/Marshmallows/MMHTTPRequest.m +++ b/Marshmallows/MMHTTPRequest.m @@ -29,7 +29,7 @@ + (id) requestWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback { - return [[[self alloc] initWithOptions: options callback: callback] autorelease]; + return [[self alloc] initWithOptions: options callback: callback]; } - (id) initWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback @@ -52,6 +52,10 @@ - (void) cancel { [_connection cancel]; + if (self.callback) { + Block_release(self.callback); + self.callback = nil; + } } - (NSData *) responseData @@ -61,9 +65,9 @@ - (NSString *) responseText { - return [[[NSString alloc] initWithBytes: _responseData.bytes - length: _responseData.length - encoding: NSUTF8StringEncoding] autorelease]; + return [[NSString alloc] initWithBytes: _responseData.bytes + length: _responseData.length + encoding: NSUTF8StringEncoding]; } - (UIImage *) responseImage @@ -96,10 +100,11 @@ - (void) connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response { +// NSLog(@"didReceiveResponse: %@",response); if ([response respondsToSelector: @selector(statusCode)]) { _statusCode = [(NSHTTPURLResponse *)response statusCode]; - _responseHeaders = [[(NSHTTPURLResponse *)response allHeaderFields] retain]; + _responseHeaders = [(NSHTTPURLResponse *)response allHeaderFields]; } else { NSLog(@"Not an HTTP response? connection: %@ response: %@", connection, response); @@ -112,19 +117,23 @@ - (void) connection: (NSURLConnection *)connection didReceiveData: (NSData *)data { +// NSLog(@"didReceiveData: %@", data); [_responseData appendData: data]; } - (void) connection: (NSURLConnection *)connection didFailWithError: (NSError *)error { - [_responseData release]; + NSLog(@"didFailWithError: %@", error); _responseData = nil; _statusCode = MMHTTPRequestStatusError; - self.callback(self.statusCode, error); + if (self.callback) { + self.callback(self.statusCode, error); + } } - (void) connectionDidFinishLoading: (NSURLConnection *)connection { +// NSLog(@"didFinishLoading: %d", self.statusCode); id data = nil; if (self.statusCode == 200) { if ([self.type isEqualToString: @"text"]) { @@ -137,23 +146,10 @@ data = self.responseData; } } - - self.callback(self.statusCode, data); -} -- (void) dealloc -{ - [_connection release]; - [_request release]; - [_method release]; - [_url release]; - [_headers release]; - [_data release]; - [_type release]; - [_callback release]; - [_responseHeaders release]; - [_responseData release]; - [super dealloc]; + if (self.callback) { + self.callback(self.statusCode, data); + } } @end