gpt4 book ai didi

iPhone 应用程序崩溃 - viewDidLoad

转载 作者:行者123 更新时间:2023-12-03 20:53:55 31 4
gpt4 key购买 nike

当我向主视图 Controller 中的 viewDidLoad 添加方法时,我的 iPhone 应用程序崩溃了:

#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];
}

我做错了什么?

最佳答案

首先,您没有对这段代码执行任何操作:

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

因此,只需执行 return questions; 就可以了。

这也意味着您在任何情况下都不应该释放此返回值。因此,摆脱这个:

[arr release];  

关于iPhone 应用程序崩溃 - viewDidLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6686657/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com