2011-03-09 108 views
0

尝试创建仅引发运行时错误的请求。启动请求的方法:NSURLRequest抛出运行时错误

- (void)loadMemberData { 

    //build URL 
    NSMutableString *url = [[NSMutableString alloc] initWithString:appDelegate.apiURL]; 
    [url appendFormat:@"&subaction=singlestat&memberID=%d", [[NSUserDefaults standardUserDefaults] integerForKey:@"memberID"]]; 

    NSURL *tempURL = [[NSURL alloc] initWithString:url]; 
    NSLog(@"URL: %@", tempURL); 

    //Create conn 
    NSURLRequest *request = [[NSURLRequest alloc] requestWithURL:tempURL]; 
    NSLog(@"%@", request); 
    //conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    //[request release]; 

} 

URL正确记录。我甚至检查过对象类型以确保一切都正确,并且一切似乎都很好。没有编译时错误或警告。堆栈跟踪&登录:

[Session started at 2011-03-09 15:02:32 -0500.] 
2011-03-09 15:02:33.807 NTR Beer Club[17702:207] URL: https://mydomain.com/path/to/file.php?action=get_app_data&subaction=singlestat&memberID=117 
2011-03-09 15:02:33.809 NTR Beer Club[17702:207] -[NSURLRequest requestWithURL:]: unrecognized selector sent to instance 0x4b02cf0 
2011-03-09 15:02:33.811 NTR Beer Club[17702:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLRequest requestWithURL:]: unrecognized selector sent to instance 0x4b02cf0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00db7be9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f0c5c2 objc_exception_throw + 47 
    2 CoreFoundation      0x00db96fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x00d29366 ___forwarding___ + 966 
    4 CoreFoundation      0x00d28f22 _CF_forwarding_prep_0 + 50 
    5 NTR Beer Club      0x00002190 -[MyStats loadMemberData] + 358 
    6 NTR Beer Club      0x000022c8 -[MyStats viewDidLoad] + 215 
    7 UIKit        0x004b64f0 -[UINib instantiateWithOwner:options:] + 1556 
    8 UIKit        0x004b8081 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168 
    9 UIKit        0x002c2943 -[UIApplication _loadMainNibFile] + 172 
    10 UIKit        0x002c34ca -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 291 
    11 UIKit        0x002cddb2 -[UIApplication handleEvent:withNewEvent:] + 1533 
    12 UIKit        0x002c6202 -[UIApplication sendEvent:] + 71 
    13 UIKit        0x002cb732 _UIApplicationHandleEvent + 7576 
    14 GraphicsServices     0x016eda36 PurpleEventCallback + 1550 
    15 CoreFoundation      0x00d99064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    16 CoreFoundation      0x00cf96f7 __CFRunLoopDoSource1 + 215 
    17 CoreFoundation      0x00cf6983 __CFRunLoopRun + 979 
    18 CoreFoundation      0x00cf6240 CFRunLoopRunSpecific + 208 
    19 CoreFoundation      0x00cf6161 CFRunLoopRunInMode + 97 
    20 UIKit        0x002c2fa8 -[UIApplication _run] + 636 
    21 UIKit        0x002cf42e UIApplicationMain + 1160 
    22 NTR Beer Club      0x00001b98 main + 102 
    23 NTR Beer Club      0x00001b29 start + 53 
    24 ???         0x00000001 0x0 + 1 
) 
terminate called after throwing an instance of 'NSException' 

回答

2

你应该做

NSURLRequest *request = [NSURLRequest requestWithURL:tempURL]; 

requestWithURL:创建NSURLRequest对象是autorelease -d。

实际的错误信息是“无法识别的选择器”,因为requestWithURL:是一个类方法,但是您正在使用它作为实例方法。

+0

啊。这是关于学习Objective-C最令人困惑的部分。记忆。何时分配。何时发布。何时启动。何时设置为零。我没有得到它。 – Chris 2011-03-09 20:21:01

+2

@Chris经验法则:只有'alloc'后面的'init';不以“new”或“create”开头的创建方法返回'autorelease'-d对象;当你'alloc'时,你必须在代码 – 2011-03-09 20:59:07

+1

的某处调用'release'或'autorelease'来感谢Shaggy Frog。我会尽力记住这一点。 – Chris 2011-03-09 22:38:11