iOS 개발 초보자는 XML 파일로 RSS 피드를 표시하는 응용 프로그램을 구현하려고합니다.NSXMLParser 및 ARC가있는 EXC_BAD_ACCESS
UITableView
클래스의 viewDidLoad
에는 데이터가로드 될 때까지 UIActivityIndicator
을 사용하고 있습니다.
하지만 앱이 메인 스레드로 돌아갈 때 parseXMLStart
함수 끝에 EXC_BAC_ACCESS
코드 2가 있습니다.
Thread 6 : 0-[NSXMLParser dealloc]
Message : EXC_BAC_ACCESS (code=2, address=0xc)
Line : 0xbb0840: movl (%eax,%ecx), %ecx
내가 어디에 내 오류가 무엇을 알고하지 않습니다 내가 왜 ... 여기에 오류 메시지가
이해하지 않습니다. 어떻게 해결할 수 있습니까? 당신의 선택이 잘못 입력처럼
=> Class Type :: UITableViewController
>> Header
@interface DataListViewController : UITableViewController {
UIActivityIndicatorView *activityView;
NSMutableArray *dataFromXML;
}
- (void)parseXMLStart;
- (void)parseXMLDone;
@end
>> Main
@implementation DataListViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"View 1";
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center = self.view.center;
[self performSelectorInBackground:@selector(parseXMLStart) withObject:nil];
[activityView startAnimating];
[activityView setHidesWhenStopped:YES];
[self.view addSubview:activityView];
}
#pragma mark - UIActivityIndicator Methods
- (void)parseXMLStart
{
// To Show the animation
sleep(1);
dataFromXML = [[NSMutableArray alloc] init];
// COMMENT TO TEST /*
[dataFromXML addObject:@"Element 1"];
[dataFromXML addObject:@"Element 2"];
[dataFromXML addObject:@"Element 3"];
// */ COMMENT TO TEST
// ------------------------------------------------------------------------------------------------------------------------------------
// UNCOMMENT TO TEST
/*
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"xml"];
NSURL *url = [[NSURL alloc] initWithString:[[NSString stringWithFormat:@"file://%@",filePath] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
XML2ObjectParser *parserWords = [[XML2ObjectParser alloc] parseXMLAtURL:url toObject:@"Word" parseError:nil];
NSLog(@">> parserWords Items Count :: %i", parserWords.items.count);
for (int i = 0; i < [parserWords.items count]-1; i++) {
Word *aWord = [[Word alloc] init];
aWord = (Word *)[[parserWords items] objectAtIndex:i];
[dataFromXML addObject:aWord];
}
NSLog(@">> dataFromXML Count :: %i", dataFromXML.count);
*/
// UNCOMMENT TO TEST
// --------------------------------------------------------------------------------------------------------------------------------------------
// EXC_BAD_ACCESS (code=2, address=0xc)
// Thread 6 : 0-[NSXMLParser dealloc]
// 0xbad840: movl (%eax,%ecx), %ecx
// --------------------------------------------------------------------------------------------------------------------------------------------
[self performSelectorOnMainThread:@selector(parseXMLDone) withObject:nil waitUntilDone:YES];
}
- (void)parseXMLDone
{
[activityView stopAnimating];
[self.tableView reloadData];
}
# pragma mark - Table View Method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataFromXML count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [dataFromXML objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DataListDetailViewController *_dataListDetailViewController = [[DataListDetailViewController alloc] init];
_dataListDetailViewController.title = [dataFromXML objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:_dataListDetailViewController animated:YES];
}
@end
몇 가지 .... 1)'XML2ObjectParser'는 왜'NSXMLParser'의 서브 클래스입니까? 그것은 하나를 사용 하나 하나로서 행동하지 않습니다. 2)'init' 메소드를 호출하지 않고'alloc'을 호출하는 것은 대개 잘못된 것입니다. 3) 당신은 빌드/실행을 위해 사용하고있는 계획에서 좀비를 켜고 Objective-C 예외에 대한 중단 점을 사용함으로써 더 나은 정보를 얻을 수 있어야합니다. –