Iphone 试图将NSURLConnection函数分离到另一个类中,但在委派时出错
我想创建一个MVC iphone应用程序,我的应用程序任务的一部分是从互联网获取数据,我想把这部分分离到一个模型中,下面是我写的Iphone 试图将NSURLConnection函数分离到另一个类中,但在委派时出错,iphone,model-view-controller,nsurlconnection,Iphone,Model View Controller,Nsurlconnection,我想创建一个MVC iphone应用程序,我的应用程序任务的一部分是从互联网获取数据,我想把这部分分离到一个模型中,下面是我写的 //Connection.h #import <Foundation/Foundation.h> @interface Connection : NSObject { NSMutableData * rawData; BOOL ready; } -(id) initWithData; -(BOOL) isDataReady; @
//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
错误是
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异常预处理+185 1 libobjc.A.dylib0x00f24313 objc_异常_抛出+44 2 CoreFoundation
0x00dd20bb-[NSObject(NSObject)不识别选择器:][187 3
CoreFoundation 0x00d41966 uuu转发+966 4芯基础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 发送操作:发送到:forEvent:+67 9 UIKit
0x00352c2b-[UIControl(内部)\u发送操作预防:带事件:]+ 527 10 UIKit 0x003517d8-[UIControl touchesEnded:withEvent:+458 11 UIKit
0x002E4ED-[UIWindow\u sendTouchesForEvent:+567 12 UIKit
0x002c5c37-[UIApplication sendEvent:+447 13 UIKit
0x002caf2e\u UIApplicationHandleEvent+7576 14图形服务
0x01728992 PurpleEventCallback+1550 15芯基础
0x00db1944CFRUNLOOP\u正在调用\u OUT\u到\u A\u SOURCE1\u PERFORM\u函数 +52 16 CoreFoundation 0x00d11cf7\uuu CFRunLoopDoSource1+215 17 CoreFoundation 0x00d0ef83\uuuu CFRunLoopRun+979 18 CoreFoundation
0x00d0e840 CFRunLoopRunSpecific+208 19核心基础
0x00d0e761 CFRUNLOOPSRUNINMODE+97 20图形服务
0x017271c4 GSEventRunModal+21721图形服务
0x01727289 GSEventRun+115 22 UIKit
0x002CE93 UIApplicationMain+1160 23测试PP
0x00001e04主+102 24测试PP
0x00001d95 start+53)在抛出的实例后调用终止 “NSException”
如何将NSURLConnection分离到另一个类?此处的代码中有一个错误:
[[NSURLConnection alloc] initWithRequest:[request delegate:self]];
应改为:
[[NSURLConnection alloc] initWithRequest:request delegate:self];
您的代码中有一个错误:
[[NSURLConnection alloc] initWithRequest:[request delegate:self]];
应改为:
[[NSURLConnection alloc] initWithRequest:request delegate:self];