2013-05-12 3 views
-1

저는 Xcode의 초보자입니다. XML 파일을 다운로드하고 파싱 한 후 배열에서 얻은 모든 요소를 ​​추가하는 앱을 만들려고했습니다. 그런 다음이 배열을 표보기에서 사용하여 새 행을 만듭니다. 불행히도 그것은 작동하지 않았다. NSLogging이 거의 매 단계마다 앱을 다운로드하고 파싱하는 것으로 나타 났지만 배열에 데이터를 추가 할 때 앱에서 배열을 비우고 마지막 요소를 여러 번 채 웁니다. 처음에는 요소. 심지어 테이블 뷰에는 일부 행이 있어야하지만 구문 분석 루틴에서 빠져 나오면 어레이가 "\ n"으로 채워져있는 것처럼 보입니다. 이해하지 못합니다. 나는 할당하고 적절히 배열을 초기화 할 : myArray = [[NSMutableArray alloc]init]; 을 그리고 난으로 배열에 객체를 추가 : 나는 엑스 코드 4.6.2을 사용 나는 정확한 것을 [myArray addObject:@"Hello"];XML 구문 분석 후 배열이 비어 있습니다.

. iOS 6의 경우 초보자입니다. (나를 쏘지 마세요.)

도움을 주셔서 감사합니다.

내가 사용하는 XML 파일이 여기에 ilgl.lgl.lu/Missings_Data/Missings_Data.xml

을 찾을 수있는 코드의 일부는 내가 사용 북두칠성에

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
if ([elementName isEqualToString:@"Released_classes"]) { 
    prof = [[NSMutableArray alloc]init]; 
    classe = [[NSMutableArray alloc]init]; 
    span = [[NSMutableArray alloc]init]; 
    service = [[NSMutableArray alloc]init]; 
    currentElement = [[NSMutableString alloc] init]; 
    NSLog(@"Data containers initialized"); 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
// ignore root and empty elements 
if ([elementName isEqualToString:@"Missing_Class"] || [elementName isEqualToString:@"Released_classes"]) return; 
if ([elementName isEqualToString:@"Classe"]) { 
    [classe addObject:currentElement]; 
    NSLog(@"Added element to Classe: %@", currentElement); 
    NSLog(@"Elements in class: %@", classe); 
    return; 
} 
if ([elementName isEqualToString:@"Professeur"]) { 
    [prof addObject:currentElement]; 
    NSLog(@"Added element to Prof: %@", currentElement); 
    NSLog(@"Elements in prof: %@", classe); 
    return; 
} 
if ([elementName isEqualToString:@"Lecon_libérée"]) { 
    [span addObject:currentElement]; 
    NSLog(@"Added element to Span: %@", currentElement); 
    NSLog(@"Elements in span: %@", classe); 
    return; 
} 
if ([elementName isEqualToString:@"Service"]) { 
    [service addObject:currentElement]; 
    NSLog(@"Added element to Service: %@", currentElement); 
    NSLog(@"Elements in service: %@", classe); 
    return; 
} 
} 

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
[currentElement setString:string]; 
} 

#pragma mark - Table View Data Organization 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
NSLog(@"Number of sections: predefined 1"); 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
NSLog(@"Number of rows: %i", [classe count]); 
return [classe count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *MyIdentifier = @"MyReuseIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier]; 
} 
NSString *cellText = [classe objectAtIndex:indexPath.row]; 
cell.textLabel.text = cellText; 
NSLog(@"Cell being created: %@", cellText); 
return cell; 
} 

편집 감사합니다 , 모든 if 조건이 parser:didEndElement:으로 끝나기 전에 currentElement = nil을 추가하고 그가 내게 준 확인. 이제 완벽하게 작동합니다! 도움을 청한 모든 분들께 감사드립니다.

+0

사용중인 코드를 게시하십시오. – Jim

+0

다운로드 한 파일 샘플이 있습니까? – Undo

+0

XMLParser 코드의 경우 Apple에서 제공 한 코드를 사용했습니다 (https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html#//apple_ref/doc/uid)./20002265-BCIJFGJI). –

답변

0

항상 currentElement을 재사용하고 값을 업데이트하고 있습니다. 값을 업데이트 할 때마다 이전 값을 덮어 씁니다 - 이전에 저장 한 모든 위치 (모든 배열에서 동일한 객체)입니다.

parser:didEndElement: 끝 부분에 currentElement = nil;을 설정하고 싶습니다. 그런 다음 parser:foundCharacters:에서 확인하고 싶습니다.

if (currentElement == nil) { 
    currentElement = [[NSMutableString alloc] initWithString:string]; 
} else { 
    [currentElement appendString:string]; 
} 
+0

문제가 해결되었습니다. 대단히 고마워, 하루 종일 내 머리를 부러 뜨 렸어! :) –

관련 문제