[英]Terminating app due to uncaught exception 'NSInvalidArgumentException
[英](Terminating app due to uncaught exception 'NSInvalidArgumentException)
我正在使用uitableview顯示json解析的數據。 解析的數據存儲在數組中,並將數組列表100分配給uitableview。 但它在{forloop}的 objectAtIndex崩潰,顯示崩潰報告為
由於未捕獲的異常“ NSInvalidArgumentException”而終止應用程序,原因:“ -[NSMutableArray insertObject:atIndex:]:嘗試在0'處插入nil對象 *
請幫助我
- (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","","","",""]}
或者您可以解析您的響應並將單個單個數據保存到另一個數組中...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.