如何从soap加载数组并在表视图上显示?

时间:2013-06-05 11:09:06

标签: ios objective-c uitableview nsmutablearray

我尝试从soap加载数组并在tableview上显示数组成员。 运行时我收到此错误:

App[711:c07] Log Output:(null)
2013-06-05 13:18:58.198 App[711:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x1736012 0x1443e7e 0x16e9b6a 0x16e9a20 0x3a511 0xf23760 0x1c91685 0x1c934e5 0x1c92f07 0xf21e02 0x3a333 0xf49589 0xf47652 0xf4889a 0xf4760d 0xf47785 0xe94a68 0x223b911 0x223abb3 0x2278cda 0x16d88fd 0x227935c 0x22792d5 0x2163250 0x16b9f3f 0x16b996f 0x16dc734 0x16dbf44 0x16dbe1b 0x27977e3 0x2797668 0x387ffc 0x2add 0x2a05 0x1)
libc++abi.dylib: terminate called throwing an exception

我的数组:NSMutableArray *BranchesNameArray;

在这里调用SOAP

NSString *mensagemSOAP= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                         "<soap:Body>\n"
                         "<GetBranches xmlns=\"http://tempuri.org/\">\n"
                         "<dt1>Africa</dt1>\n"
                         "</GetBranches>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n"];

NSLog(@"SOAP Msg = \n%@\n\n", mensagemSOAP);

NSURL *url = [NSURL URLWithString:@"http://servis.com:1249/service.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    NSString *tamanhoMensagem = [NSString stringWithFormat:@"%d", [mensagemSOAP length]];

[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/GetBranches" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:tamanhoMensagem forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[mensagemSOAP dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *conexao = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if(conexao){
    webData = [NSMutableData data];
}else{
    NSLog(@"Connection Error.");
}


 }

didStart就在这里

 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{


 if ( [elementName isEqualToString:@"branchname"] ) {

 NSLog(@"Log Output%@",[attributeDict objectForKey:@"branchname"]);
 [BranchesNameArray addObject:[attributeDict objectForKey:@"branchname"]];


teveRetorno = YES;


 }

didEnd就在这里

 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:   
  (NSString *)namespaceURI qualifiedName:(NSString *)qName{
  if ( [elementName isEqualToString:@"branchname"] ) {

 [[self tableView]reloadData];

 teveRetorno = NO;

 }
 }

表视图在这里

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
 if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:@"UITableViewCell"];
  }

  cell.textLabel.text =[BranchesNameArray objectAtIndex:indexPath.row];

 return cell;
 }

3 个答案:

答案 0 :(得分:1)

 [BranchesNameArray addObject:[attributeDict objectForKey:@"branchname"]];

您不能向数组添加null,请确保添加的值不为空

if([attributeDict objectForKey:@"branchname"])
{
     [BranchesNameArray addObject:[attributeDict objectForKey:@"branchname"]];
}

编辑首先将您的数组分配为

NSMutableArray *BranchesNameArray=[[NSMutableArray alloc]initWithCapacity:3];

答案 1 :(得分:1)

在解析数组之前,检查数组计数是否> 0

喜欢

if([BranchesNameArray count] >0){
//do your operation
}

答案 2 :(得分:1)

Problum是你的add-object为Nil值,这就是你得到错误的原因,所以在你添加Dictionary keyVlaue之前你必须检查它是否为null或者不像bellow示例可能对你有用,它可以帮助你。

    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{


     if ( [elementName isEqualToString:@"branchname"] ) {

          NSLog(@"Log Output%@",[attributeDict objectForKey:@"branchname"]);
          NSString *strValue= [attributeDict objectForKey:@"branchname"];

          if(strValue != (NSString*)[NSNull null])
          {
             [BranchesNameArray addObject:[attributeDict objectForKey:@"branchname"]];
          } 
          else
          {

          }

          teveRetorno = YES;


 }

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:   
  (NSString *)namespaceURI qualifiedName:(NSString *)qName{
  if ( [elementName isEqualToString:@"branchname"] ) {
 NSLog(@"Log array%@",BranchesNameArray);
 [[self tableView]reloadData];

 teveRetorno = NO;

  }
 }

现在您可以使用下面的方法将数组加载到表视图中: -

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.BranchesNameArray count];


}


// Customize the appearance of table view cells.
-- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
 if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:@"UITableViewCell"];
  }

  cell.textLabel.text =[BranchesNameArray objectAtIndex:indexPath.row];

 return cell;
 }

注意

在您的肥皂方法调用

时,不要忘记分配BranchesNameArray