我的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