Trying to separate the NSURLConnection function into another class but get error on delegation

0

I want to create a MVC iphone app , a part of my app task is to get data from internet and I want to separate that part to a Model and here is what I've written

//Connection.h
#import <Foundation/Foundation.h>


@interface Connection : NSObject 
{
    NSMutableData * rawData;
    BOOL ready;
}


-(id) initWithData;
-(BOOL) isDataReady;

@end


//Connection.m
#import "Connection.h"


@implementation Connection

-(id) initWithData
{
    self = [super init];
    if(self != nil)
    {
        ready = NO;
        rawData = [[NSMutableData alloc]init];
        NSString * urlString = [NSString stringWithString:@"http://www.google.com"];
        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
        [[NSURLConnection alloc] initWithRequest:[request delegate:self]];

        [rawData setLength:0];

        return self;
    }
    else {
        return nil;
    }
}
//I have implemented the connectiondidRecievedData and connectionDidFinishLoading also they are empty

but when I want to call this class from my controller I'll get error

Connection * cn = [[Connection alloc] initWithData];

and the error is

2012-01-13 15:26:59.453 TestApp[20105:207] -[NSURLRequest delegate:]: unrecognized selector sent to instance 0x4e1f520 2012-01-13 15:26:59.455 TestApp[20105:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLRequest delegate:]: unrecognized selector sent to instance 0x4e1f520' * Call stack at first throw: ( 0 CoreFoundation 0x00dd05a9 exceptionPreprocess + 185 1 libobjc.A.dylib
0x00f24313 objc_exception_throw + 44 2 CoreFoundation
0x00dd20bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3
CoreFoundation 0x00d41966 __forwarding
+ 966 4 CoreFoundation 0x00d41522 _CF_forwarding_prep_0 + 50 5 TestApp 0x0000257c -[Connection initWithData] + 339 6 TestApp
0x000020c3 -[TestAppViewController doTestB] + 122 7 UIKit
0x002c04fd -[UIApplication sendAction:to:from:forEvent:] + 119 8
UIKit 0x00350799 -[UIControl sendAction:to:forEvent:] + 67 9 UIKit
0x00352c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 10 UIKit 0x003517d8 -[UIControl touchesEnded:withEvent:] + 458 11 UIKit
0x002e4ded -[UIWindow _sendTouchesForEvent:] + 567 12 UIKit
0x002c5c37 -[UIApplication sendEvent:] + 447 13 UIKit
0x002caf2e _UIApplicationHandleEvent + 7576 14 GraphicsServices
0x01728992 PurpleEventCallback + 1550 15 CoreFoundation
0x00db1944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 16 CoreFoundation 0x00d11cf7 __CFRunLoopDoSource1 + 215 17 CoreFoundation 0x00d0ef83 __CFRunLoopRun + 979 18 CoreFoundation
0x00d0e840 CFRunLoopRunSpecific + 208 19 CoreFoundation
0x00d0e761 CFRunLoopRunInMode + 97 20 GraphicsServices
0x017271c4 GSEventRunModal + 217 21 GraphicsServices
0x01727289 GSEventRun + 115 22 UIKit
0x002cec93 UIApplicationMain + 1160 23 TestApp
0x00001e04 main + 102 24 TestApp
0x00001d95 start + 53 ) terminate called after throwing an instance of 'NSException'

how can I separate the NSURLConnection to another class?

iphone
model-view-controller
nsurlconnection

1 Answer

3

You've got an error in your code here:

[[NSURLConnection alloc] initWithRequest:[request delegate:self]];

That should read:

[[NSURLConnection alloc] initWithRequest:request delegate:self];
answered on Stack Overflow Jan 13, 2012 by mattjgalloway

User contributions licensed under CC BY-SA 3.0