2013-06-05 54 views
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; 
} 
+0

在向數組添加對象之前,請檢查該對象是否爲val ID一個。 – Anupdas

回答

1

Problum是你添加對象爲無值,這就是爲什麼你得到錯誤,所以你添加字典keyVlaue之前,你必須檢查它是零或不像波紋管的例子可能對你有用,它可以幫助你。

-(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,而你的SOAP方法調用

+0

非常感謝你,但Log Output(null)終止應用程序由於未捕獲異常'NSInvalidArgumentException',原因:'*** - [__ NSArrayM insertObject:atIndex:]:object can not be n' – Mhmt

+0

刪除NSlog沒有必要它 –

+0

我刪除了日誌,但它給了同樣的錯誤friend.When表視圖打開應用程序停止並崩潰 – Mhmt

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

不能添加空數組請務必添加值不爲空

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

編輯分配第一您的數組作爲

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

可以將一個NSNull的實例添加到數組中,以消除可以說爲零的歧義。 – Anupdas

+0

branchname不爲空值。我可以在其他日誌中看到 – Mhmt

+0

@MehmetAkyel在開始時初始化數組,在答案中。更新了答案 –

1

前解析數組檢查數組計數是否> 0

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

OK.I檢查了我的數組,它是空的。如何用我的代碼加載我的數組? – Mhmt

相關問題