私のiPhoneアプリがクラッシュ私は私のメインのViewControllerでの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