Thursday, 22 August 2013

NSURLConnection Results in BAD_ACCESS

NSURLConnection Results in BAD_ACCESS

I'm getting BAD_ACCESS when attempting to instantiate an NSURLConnection
in a class that's called from my view controller.
I've implemented the delegate methods in the class, and the interface is a
subclass of NSObject <NSURLConnectionDelegate>.
I can't for the life of me figure out what the problem is:
NSURL *url = [NSURL URLWithString:@"http://valid-value-here"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init ];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json;chartset=utf-8"
forHTTPHeaderField:@"Content-Type"];
NSString *myJSONString = @"valid-json-string";
NSData *myJSONData = [myJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger cCount = [myJSONString length];
[request addValue:[NSString stringWithFormat:@"%d", cCount]
forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:myJSONData];
NSURLResponse *theResponse = [[NSURLResponse alloc]init];
NSError *theError = nil;
NSURLConnection *theConnection = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
When the code hits the last line above, the exception occurs.
I'm a .Net developer most of the time, so my familiarity with XCode is
sub-par, and I'm also relatively new to Objective-C, so any help is
appreciated!

No comments:

Post a Comment