0

我尝试从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;
 }
4

3 回答 3

1

Problum is you are add-object as Nil value that's why you getting error, So before you add Dictionary keyVlaue you have to check it is nil or not like bellow example may be useful for you and it helps you.

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

  }
 }

Now you can load your array into your table view using bellow method:-

#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;
 }

NOTE

Do not forget to allocate your BranchesNameArray while your soap Method calling

于 2013-06-05T12:14:41.420 回答
1

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

喜欢

if([BranchesNameArray count] >0){
//do your operation
}
于 2013-06-05T12:03:38.733 回答
1
 [BranchesNameArray addObject:[attributeDict objectForKey:@"branchname"]];

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

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

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

NSMutableArray *BranchesNameArray=[[NSMutableArray alloc]initWithCapacity:3];
于 2013-06-05T11:12:07.500 回答