(终止应用程序,由于未捕获的异常'NSInvalidArgumentException)
我正在使用uitableview来显示json解析的数据。解析的数据存储在数组中,并且数组列表100被分配给uitableview。但它摔碎在objectAtIndex 在{for循环}它显示崩溃报告(终止应用程序,由于未捕获的异常'NSInvalidArgumentException)
终止应用程序由于未捕获的异常 'NSInvalidArgumentException',理由是:“ - [NSMutableArray的insertObject:atIndex:]:尝试插入零在0'对象*
普莱舍帮助我
- (void)viewDidLoad
{
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:openexchangeURl]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
self.responseData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
values = [responseString JSONValue];
array = [[NSMutableArray alloc] init];
NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
NSMutableArray *arrValues = [[NSMutableArray alloc] init];
array =[values valueForKey:@"rates"];
NSLog(@"array values:--> %@",array);
// NSLog(@"values:--> %@",values);
// NSLog(@"Particular values:--> %@",[[values valueForKey:@"rates"] valueForKey:@"AED"]);
tempDict1 = (NSMutableDictionary *)array;
NSArray *arr;// =[[NSArray alloc]init];
arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"];
NSLog(@"arr-->%@",arr);
NSString *subStar = @"=";
[arrTitle removeAllObjects];
[arrValues removeAllObjects];
for (int i=0; i<[arr count]-1; i++)
{
[arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
[arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
NSLog(@"arrTitle is:--> %@",arrTitle);
}
tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
array = [values valueForKey:@"rates"];
NSLog(@"tempDict--%@",[tempDict1 objectForKey:@"AED"]);
[array retain];
[tbl_withData reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"array-->%@",array);
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
intIndexPath = indexPath.row;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:8];
cell.textLabel.numberOfLines = 4;
}
// NSLog(@"data is like:--> %@",array);
// cell.textLabel.text= [NSString stringWithFormat:@"%@",[array objectAtIndex:intIndexPath]];
cell.textLabel.text =[array objectAtIndex:intIndexPath];
return cell;
}
还有就是你的数组中只有一个项目,在这里你刚才添加的一个对象:
array = [[NSMutableArray alloc] init];
[array addObject:[values valueForKey:@"rates"]];
您应该将从[values valueForKey:@"rates"]
获得的值分配给数组变量。还要使数组变量成为保留该值的属性。
@property (nonatomic, strong) NSArray *array;
然后将其分配给属性:
self.array = [values valueForKey:@"rates"];
而且将单元格的所有造型到这里,如果你创建新的细胞。这会加快速度,因为你不会每次都改变单元格的样式。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:8];
cell.textLabel.numberOfLines = 4;
}
NSLog(@"data is like:--> %@",array);
// NSString *cellValue =[array objectAtIndex:indexPath.row];
// cell.textLabel.text = cellValue;
cell.textLabel.text= [NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]];
return cell;
}
以正确的格式从服务器获取数据...在您的情况下,您的数据位于一个数组中。所以使它在不同的不同阵列..
{"reply":["AED = 3.673188","AFN = 48.5725","","","",""]}
,或者你可以分析你的反应和单一单数据保存到另一个数组...
{AED =“3.672563”; AFN =“48.723751”; ALL =“107.146666”; AMD =“411.284994”; ANG =“1.78035”; AOA =“95。394043“; ARS =”4.6604“; .....} – anilkumar07
所以现在要打印什么......只是数字(3.672563)或在您的行中完成(AED =”3.672563“) – Rajneesh071
我想打印Aed =“3.672563” – anilkumar07
您的代码工作正常....是什么问题? – Rajneesh071
在单个可用视图单元 – anilkumar07
中显示的所有抓取数据(列表100)什么是[values valueForKey:@“rates”]的outut。 – Rajneesh071