如何使用字典将值存储在数组中,用于 soap Web 服务 ios



这里 NsmutableString=textInProgress, count1 是计数器管理的。

- (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;
 }
  • 拜托,有人指导我如何将这个字符串值存储在数组中?在这里,当我在日志中打印textInProgress时,它返回空值。

输出的某些部分:-

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

在任何其他方法中,我没有增加count1变量的此方法

你已经创建了解析器的nsobject文件

解析器.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

现在解析器.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

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是另一个nsobject类,它有助于处理数据。 书籍详情.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

要获取有关表单元格的数据,请单击可能会对您有所帮助..

     - (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];

         }

最新更新