-1

我是 IOS 新手,我需要在我的程序中实现 NSThread,但是当它调用它时会显示 SIGABRT 错误。我当前的代码如下

XMLParser.m

-(void)loadXML
{
    categories =[[NSMutableArray alloc]init];
    NSString *filepath =[[NSBundle mainBundle]pathForResource:@"cd_catalog" ofType:@"xml"];
    NSData *data =[NSData dataWithContentsOfFile:filepath];
    parser=[[NSXMLParser alloc]initWithData:data];
    parser.delegate =self;
    [parser parse];
}

视图控制器.m

- (void)viewDidLoad
{
    xmlParser =[[XMLParser alloc]init];
    NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(loadXML) object:nil];
    [myThread start];
    [super viewDidLoad];
}

请告诉我我的程序有什么问题

4

4 回答 4

2

使用此代码解决您的问题...

视图控制器.m

- (void)viewDidLoad
{
    NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(doParsing) object:nil];
    [myThread start];
    [super viewDidLoad];
}
-(void)doParsing
{
    xmlParser =[[XMLParser alloc]init];
    [xmlParser loadXML];
}
于 2013-03-18T05:29:34.383 回答
0

NSThread您可以使用启动线程而不是创建对象

//performSelectorInBackground:withObject: is NSObject's method
[self performSelectorInBackground:@selector(loadXML) withObject:nil];

我没有发现任何错误代码,但启用NSZombie并查看导致此问题的对象。

于 2013-03-18T05:27:08.953 回答
0

loadXML 未在 ViewController 上定义,因此您的线程代码应更改为使用 XMLParser 的实例而不是 self ,如下所示:

XMLParser *parser = [[XMLParser alloc] init];
NSThread *thread = [[NSThread alloc] initWithTarget:parser selector:@selector(loadXML) object:nil];
[thread start];
于 2013-03-18T05:33:13.913 回答
0

由于 Apple 引入了 GCD,您可以在不创建任何NSThread实例的情况下解决它。

dispatch_async(dispatch_get_global_object(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  [self loadXML];
});
于 2015-09-25T08:26:49.810 回答