2016-02-26 35 views
6

Tutaj NmutableString = textInProgress, count1 jest zarządzane przez licznik.Jak przechowywać wartość w tablicy ze słownikiem dla usługi sieciowej mydła ios

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    NSLog(@"%d",count1); 

    // Build the text value 
    [textInProgress appendString:string]; 
    NSLog(@"%@",textInProgress); 

    NSLog(@"%d",count1); 
    count1=count1+1; 
} 
  • proszę, ktoś poprowadzi mnie jak przechowywać tą wartość w tablicy ?. I tutaj, gdy wydrukuję textInProgress w dzienniku, Zwraca wartość null.

pewna część produkcji: -

2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 0 
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] (null) 
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 0 
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 2 
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] (null) 
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 2 

Zamiast tej metody w jakikolwiek inny sposób i nie uczynić przyrosty count1 zmiennej

Odpowiedz

5

u mają tworzyć NSObject plik parser

parser.h

#import <Foundation/Foundation.h> 
#import "BookDetail.h" 
#import "AppDelegate.h" 

@interface Parser : NSObject<NSXMLParserDelegate> 
{ 
AppDelegate *app; 

NSMutableString *character; 
BookDetail *book; 
NSString *tempAuthorName; 
NSMutableArray *booksArray; 


} 
-(Parser *)initParser; 
@property(strong,nonatomic)NSString *tempAuthorName; 
@property (nonatomic,retain) NSMutableArray *booksArray; 
@end 

teraz parser.m

#import "Parser.h" 
    #import "ViewController.h" 
    #import "DisplayInfoViewController.h" 
    @implementation Parser 
    @synthesize booksArray,tempAuthorName; 


-(Parser *)initParser 
{ 

    self = [super init]; 

    return self; 
    } 
    -(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
    { 
    if([elementName isEqualToString:@"books"]) 
    { 
    booksArray = [[NSMutableArray alloc]init]; 
    } 
    else if ([elementName isEqualToString:@"book"]) 
    { 
    book = [[BookDetail alloc]init]; 
    } 



    } 
    -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    { 

     string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    if (!character) 

    character=[[NSMutableString alloc] initWithString:string]; 

else 
    [character appendString:string]; 
} 
     -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    { 

if([elementName isEqualToString:@"isbn"]) 
{ 
    book.isbn = character; 

} 
else if ([elementName isEqualToString:@"title"]) 
{ 
    book.title = character; 
} 
else if ([elementName isEqualToString:@"author"]) 
{ 
    book.author = character; 
} 

else if ([elementName isEqualToString:@"publisher"]) 
{ 
    book.publisher = character; 
} 

else if ([elementName isEqualToString:@"amazon_price"]) 
{ 
    book.amazon_price = character; 
} 
else if ([elementName isEqualToString:@"book"]) 
{ 

    [booksArray addObject:book]; 


} 

character = nil; 



    } 
    @end 

plik xml

<?xml version="1.0"?> 
    <books> 
    <book> 
     <isbn>1594489501</isbn> 
     <title>A Thousand Splendid Suns</title> 
     <author>Romio</author> 
     <publisher>Riverhead Hardcover</publisher> 
    <amazon_price>14.27</amazon_price> 
    </book> 
    <book> 
    <isbn>1594489587</isbn> 
    <title>The Brief Wondrous Life of Oscar Wao</title> 
    <author>Junot Diaz</author> 
    <publisher>Riverhead Hardcover</publisher> 
    <amazon_price>14.97</amazon_price> 
</book> 
<book> 
    <isbn>0545010221</isbn> 
    <title>Harry Potter and the Deathly Hallows</title> 
    <author>J. K. Rowling</author> 
    <publisher>Arthur A. Levine Books</publisher> 
    <amazon_price>19.24</amazon_price> 
</book> 
</books> 

BookDetail jest kolejną klasę NSObject które przyczyniają się do przenoszenia danych BookDetail.h

#import <Foundation/Foundation.h> 

    @interface BookDetail : NSObject 
    @property(nonatomic,retain)NSString *isbn; 
    @property(nonatomic,retain)NSString *title; 
    @property(nonatomic,retain)NSString *author; 
    @property(nonatomic,retain)NSString *publisher; 
    @property(nonatomic,retain)NSString *amazon_price; 


    @end 

.m

#import "BookDetail.h" 

    @implementation BookDetail 
    @synthesize isbn,title,author,publisher,amazon_price; 

    @end 

Dla uzyskania danych na komórki tabeli kliknięcie może to pomoże ..

 - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 


      static NSString *identifier = @"simpleIdentifier"; 
      TableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifier]; 
      if(cell == nil) 
      { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
      } 
      BookDetail *ss=[parse.booksArray objectAtIndex:indexPath.row]; 
     cell.bookName.text=ss.author; 
     return cell; 
     } 

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
      DisplayInfoViewController *displayinfoViewController=[[DisplayInfoViewController alloc] initWithNibName:@"DisplayInfoViewController" bundle:nil]; 
      BookDetail *ss=[parse.booksArray objectAtIndex:indexPath.row]; 
     displayinfoViewController.isbnValue=ss.isbn; 
     displayinfoViewController.titleValue=ss.title; 
     displayinfoViewController.amazonValue=ss.amazon_price; 
     displayinfoViewController.authorValue=ss.author; 
     displayinfoViewController.publisherValue=ss.publisher; 


     [self.navigationController pushViewController:displayinfoViewController animated:YES]; 



     } 
+0

mi nic powiedzieć, jeśli chcesz .... –

+0

jeśli jestem próbować ten mój wniosek zostanie cresh dół: - 'BookDetails * ss = [booksArray objectAtIndex: 2]; ' –

+0

Najpierw prześledzę drukowanie w usłudze nslog, ale wniosek zostanie usunięty z błędem: =' Kończenie aplikacji z powodu nierozszyfrowanego wyjątku 'NSRangeException', powód: "*** - [__ NSArrayM objectAtIndex:]: index 2 poza granicami [0 .. 0] '' –