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

Posted

技术标签:

【中文标题】如何使用soap Web服务ios的字典将值存储在数组中【英文标题】:How to store value in array with dictionary for soap web service ios 【发布时间】:2016-06-09 14:50:04 【问题描述】:

这里 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 变量

【问题讨论】:

【参考方案1】:

你已经创建了解析器的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 类 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

为了获取表格单元格上的数据,点击可能会对您有所帮助..

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



         

【讨论】:

如果我尝试这个我的应用程序将会崩溃:- BookDetails *ss=[booksArray objectAtIndex:2]; 我首先尝试在 nslog 中打印,但应用程序因错误而崩溃:=Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 0]' 在 booksArray 上放断点并检查。他们的索引是多少 我有一个简单的问题是每当我存储价值book.title=character,它会覆盖旧值..请给出一些解决方案 NSString* path = [[NSBundle mainBundle] pathForResource:@"books" ofType:@"xml"]; NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path]; NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:xmlData]; parse = [[Parser alloc]initParser]; [nsXmlParser setDelegate:parse]; BOOL 成功 = [nsXmlParser 解析];

以上是关于如何使用soap Web服务ios的字典将值存储在数组中的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Java 类调用 SOAP Web 服务?

在 iOS 中解析 JSON 并将结果存储到 Array

使用 JSON Patch 将值添加到字典

需要了解PHP中的SOAP 1.2 Web服务数据调用/发送

SOAP Web 服务不仅适用于 IOS 设备,它还适用于 soap ui 和 Android 设备

如何在 iOS 中禁用来自 Web 服务调用的自动排序 JSON 字典响应