Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store value in array with dictionary for soap web service ios

Here NsmutableString=textInProgress, count1 is counter managed.

- (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;
 }
  • Please, someone guide me how to store this string value in array?. And here when i print textInProgress in log, It return null value.

Some portion Of Output:-

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

Instead of this method in any other method i have not make increments of count1 variable

like image 307
Smit Saraiya Avatar asked Feb 26 '16 11:02

Smit Saraiya


1 Answers

u have create nsobject file of 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

now 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

xml file

   <?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 is another nsobject class which help to handling the data 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

For getting data on table cell click may it will help you..

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



         }
like image 73
Nishant Gupta Avatar answered Sep 26 '22 14:09

Nishant Gupta