iPhone应用程序崩溃-viewDidLoad
当我向主viewcontroller中的viewDidLoad添加方法时,我的iphone应用程序崩溃:iPhone应用程序崩溃-viewDidLoad,iphone,objective-c,xcode,Iphone,Objective C,Xcode,当我向主viewcontroller中的viewDidLoad添加方法时,我的iphone应用程序崩溃: #import "dbQuestionGetterViewController.h" #import "dbConnector.h"; @implementation dbQuestionGetterViewController @synthesize questions; -(void)viewDidLoad{ //code to initialise view NSD
#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];
}
我做错了什么?您正在释放
返回的arr
对象。由于您已经将此对象标记为autorelease
,因此不需要自己显式释放它。从viewDidLoad
中删除以下行,您应该被设置:
[arr release];
此外,您的myRequestString
也是可疑的。您正在调用string
类方法,该方法返回一个完全分配和初始化的字符串,但随后您在其上调用initWithFormat
,这通常适用于刚刚alloc
-ed的字符串。将该行替换为:
[[NSMutableString alloc]initWithFormat:@"section=%@", sectionId]
然后在[request release]
从静态方法返回的NSDictionary被自动删除后立即释放它。但是您可以在viewDidLoad方法中释放它。首先,您没有使用此代码执行任何操作:
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];
移除它
其次,问题
已经自动删除
NSDictionary *questions = [json objectFromJSONString]; // < autoreleased
你能发布堆栈跟踪吗?请原谅我的无知,但我该怎么做:崩溃后,SIt应该出现在调试器窗口中。你从我在另一个问题中留下的答案中得到了代码。如果它对你有帮助,请接受它。我为此花了不少时间/你做了很多错事。对于初学者,将[super viewDidLoad]调用放在方法的顶部。您还将自动释放的对象传递回您的方法,将其分配给另一个变量,然后释放它。当您执行questions=arr
,[arr release]
时,您的questions对象指向的内存位置与现在的内存位置相同,该内存位置是垃圾。再看看您的myRequestString
初始化,您没有正确初始化它。前几行是我用来将数据发布到数据库的。在做了更改之后,程序仍然崩溃……你是否忽略了我在后面的回答中所说的内容?这是您的问题。请在进行更改后立即复制/粘贴您拥有的内容。把它放在上面,如果你给它一个链接,它会有帮助的。这里是:请把这个链接作为编辑放在你的原始帖子中,这样其他人就可以看到完整的实现并帮助你。
[arr release];