Ios NSThread显示SIGABRT
我是IOS新手,我需要在程序中实现NSThread,但当它被调用时,会显示一个SIGABRT错误。我目前的代码如下 XMLParser.mIos NSThread显示SIGABRT,ios,objective-c,Ios,Objective C,我是IOS新手,我需要在程序中实现NSThread,但当它被调用时,会显示一个SIGABRT错误。我目前的代码如下 XMLParser.m -(void)loadXML { categories =[[NSMutableArray alloc]init]; NSString *filepath =[[NSBundle mainBundle]pathForResource:@"cd_catalog" ofType:@"xml"]; NSData *data =[NSData
-(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];
}
ViewController.m
- (void)viewDidLoad
{
xmlParser =[[XMLParser alloc]init];
NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(loadXML) object:nil];
[myThread start];
[super viewDidLoad];
}
请告诉我我的程序有什么问题,而不是创建一个
NSThread
对象,您可以使用
//performSelectorInBackground:withObject: is NSObject's method
[self performSelectorInBackground:@selector(loadXML) withObject:nil];
我没有发现任何错误代码,但请查看是哪个对象导致了此问题。使用此代码解决您的问题 ViewController.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];
}
loadXML未在ViewController上定义,因此您的线程代码应更改为使用XMLParser实例,而不是像自己一样:
XMLParser *parser = [[XMLParser alloc] init];
NSThread *thread = [[NSThread alloc] initWithTarget:parser selector:@selector(loadXML) object:nil];
[thread start];
由于Apple引入了GCD,您可以在不创建任何
NSThread
实例的情况下解决它
dispatch_async(dispatch_get_global_object(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self loadXML];
});
您正在使用ARC吗?在
viewDidLoad
内部的viewDidLoad
之后,您的应用程序在哪里崩溃?是的,我正在使用ARC,当它到达nsthread实现时,它会崩溃。这可能是由于运行循环。你能在loadXML
中开始运行循环吗?如何…我不知道如何实现它,因为我说过我正在努力研究这个问题self@AlenJoy您需要提供有关错误的更多详细信息。您很可能收到一条消息,说明对特定类型的对象调用了无效选择器。该消息必须是[xmlParser performSelectorInBackground:@selector(loadXML)withObject:nil]代码>