use ARC, remove retain/release/autorelease

This commit is contained in:
Sami Samhuri 2012-11-14 22:06:49 -08:00
parent 0fe10f0abb
commit 348909fc04
3 changed files with 33 additions and 40 deletions

View file

@ -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

View file

@ -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;

View file

@ -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