【发布时间】:2014-08-01 07:25:19
【问题描述】:
我在每个单元格中都有一个带有文本框的集合视图。我正在从 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;
}
在这种情况下,我的应用程序崩溃显示错误 ....
由于未捕获的异常而终止应用程序'
NSInvalidArgumentException',原因:'-[__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;
}
这次应用程序崩溃再次显示错误
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序, 原因:'-[__NSCFConstantString stringValue]:无法识别的选择器 发送到实例。
我该如何解决这个问题?
【问题讨论】:
-
你可以尝试使用 NSString *myStringWithNumbers = [NSString stringWithFormat:@"%d",[currenymap objectForKey:_currencyRateValueDetails[row]]];
-
@user2071152 感谢您的工作。如果您将评论作为答案,我将设置为正确答案。
-
@user2071152 我需要更多帮助才能像这种方法一样将 NSString 转换为 NSNumber
-
对于以整数开头的字符串,例如 @"123" 等,使用 -[NSString integerValue]。 (NSInteger number=[string integerValue];)。你也可以使用 NSNumberFormatter 作为 NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *myNumber = [f numberFromString:stringNum]; [f 发布];
标签: ios iphone objective-c nsstring nsdictionary