我的iPhone应用程序崩溃时,我加入到viewDidLoad中的方法在我的主视图控制器:iPhone应用程序崩溃 - viewDidLoad中
#import "dbQuestionGetterViewController.h"
#import "dbConnector.h";
@implementation dbQuestionGetterViewController
@synthesize questions;
-(void)viewDidLoad{
//code to initialise view
NSDictionary* arr = [dbConnector getQuestions:2 from:@"http://dev.speechlink.co.uk/David/get_questions.php"];
questions = arr;
[arr release];
[super viewDidLoad];
}
我打电话从dbConnector类的静态方法,但它崩溃之前,甚至装..
在dbConnector的方法:
//method to
+(NSDictionary*)getQuestions:(NSInteger)sectionId from: (NSString*) url{
//connect to database given by url
//NSError *error = nil;
//NSURLResponse *response = nil;
NSMutableString* myRequestString = [[NSMutableString string]initWithFormat:@"section=%@", sectionId];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];
//post section
[request setHTTPBody: myRequestData];
//store them in the dictionary
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *questions = [json objectFromJSONString];
[json release];
[request release];
return [questions autorelease];
}
我到底做错了什么?
你从我在你的其他问题留下的答案得到的代码。请接受它,如果它帮助你。我花了不少时间。 :/ –
你做错了很多事情。将[super viewDidLoad]调用放置在方法的顶部,供初学者使用。您还将自动释放对象传递回您的方法,将其分配给另一个变量,然后释放它。当你做'questions = arr','[arr release]'时,你的问题对象指向同样的内存位置,这个位置现在是垃圾。也看看你的'myRequestString'初始化,你没有正确地启动它。 – Rog