0

ソープから配列を読み込んで、配列のメンバーをテーブルビューに表示しようとしました。実行すると、次のエラーが表示されます。

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

問題は、オブジェクトを nil 値として追加しているため、エラーが発生することです。そのため、Dictionary keyVlaue を追加する前に、それが nil であるか、次の例とは異なるかを確認する必要があります。

    -(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ソープメソッドの呼び出し中に割り当てを忘れないでください

于 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 に答える