2012-01-13 34 views
0

我想創建一個MVC iphone應用程序,我的應用程序任務的一部分是從互聯網獲取數據,並且我想將該部分分離到一個模型,然後在此處就是我寫嘗試將NSURLConnection函數分離爲另一個類,但在委派時出錯

//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 delegate:]: 無法識別的選擇器發送到實例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 - [連接initWithData] + 339 6 TestApp
0x000020c3 - [TestAppViewController doTestB] + 122 7的UIKit
0x002c04fd - [UIApplication的sendAction:到:從:forEvent:] + 119 8
UIKit的0x00350799 - [UIControl sendAction:至:forEvent:] + 67 9的UIKit
0x00352c2b - [UIControl(內部)_sendActionsForEvents:withEvent:方法] + 527 10的UIKit 0x003517d8 - [UIControl 到uchesEnded: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主+ 102 24 TestApp
0x00001d95啓動+ 53)之後終止稱爲拋出一個實例 'NSException'

如何將NSURLConnection分隔到另一個類?

回答

3

你已經在你的代碼在這裏得到了一個錯誤:

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

這應該閱讀:

[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
相關問題