尝试将NSURLConnection函数分离到另一个类,但在委派时会出错

时间:2012-01-13 15:30:20

标签: iphone model-view-controller nsurlconnection

我想创建一个MVC iphone应用程序,我的应用程序任务的一部分是从互联网获取数据,我想将该部分与模型分开,这就是我写的

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

但是当我想从我的控制器调用这个类时,我会收到错误

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

,错误是

  

2012-01-13 15:26:59.453 TestApp [20105:207] - [NSURLRequest委托:]:   无法识别的选择器发送到实例0x4e1f520 2012-01-13   15:26:59.455 TestApp [20105:207] *由于未被捕获而终止应用程序   异常'NSInvalidArgumentException',原因:' - [NSURLRequest   委托:]:无法识别的选择器发送到实例0x4e1f520'   * 首次调用堆栈:(0 CoreFoundation 0x00dd05a9 exceptionPreprocess + 185 1 libobjc.A.dylib
  0x00f24313 objc_exception_throw + 44 2 CoreFoundation
  0x00dd20bb - [NSObject(NSObject)doesNotRecognizeSelector:] + 187 3
  CoreFoundation 0x00d41966 __ 转发
+ 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(内部)_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图形服务
  0x017271c4 GSEventRunModal + 217 21 GraphicsServices
  0x01727289 GSEventRun + 115 22 UIKit
  0x002cec93 UIApplicationMain + 1160 23 TestApp
  0x00001e04 main + 102 24 TestApp
  0x00001d95 start + 53)在抛出一个实例后终止调用   'NSException'

如何将NSURLConnection与另一个类分开?

1 个答案:

答案 0 :(得分:3)

您的代码中出现错误:

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

那应该是:

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