如何在ios中解决数据类型问题

时间:2014-06-11 09:37:43

标签: ios iphone objective-c nsstring nsdictionary

我在每个单元格中都有一个带有文本框的集合视图。我从一个nsmutable字典中获取值,并使用以下代码在单元格中设置文本框文本。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    NSDictionary *datadictionary = [_currencyDictionary objectForKey:_currencyNameDetails[_flag]];
    cell.currenyRate.text = [datadictionary objectForKey:_currencyRateValueDetails[row]];
    return cell;

}

在这种情况下,我的应用程序崩溃显示错误....

由于未捕获的异常而终止应用<&#39;

  

NSInvalidArgumentException&#39;,原因:&#39; - [__ NSCFNumber长度]:   无法识别的选择器发送到实例。

我找到了使用下面提到的方法的原因。[datadictionary objectForKey:_currencyRateValueDetails [row]]对象是nsnumber格式。

if([[datadictionary objectForKey:_currencyRateValueDetails[row]] isKindOfClass:[NSNumber class]])
    {
        NSLog(@"nsnumber......");
    }
    else{NSLog(@"nsstring......");
}

所以将我的代码重写为

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView   cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

     CollectionviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    NSDictionary *datadictionary = [_currencyDictionary objectForKey:_currencyNameDetails[_flag]];
    NSNumber *Value =[[NSNumber alloc]init];
    Value = [currenymap objectForKey:_currencyRateValueDetails[row]];
    NSString *myString = [Value stringValue];
    cell.currenyRate.text = myString;
    return cell;


}

这次应用程序崩溃再次显示错误

  

由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,   原因:&#39; - [__ NSCFConstantString stringValue]:无法识别的选择器   发送到实例。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用

NSString *myStringWithNumbers = [NSString stringWithFormat:@"%d",[currenymap objectForKey:_currencyRateValueDetails[row]]];

希望这有帮助。

相关问题