user3698427
user3698427

Reputation: 1

How to Solve Datatype issue in ios

I have one collection view with textbox in each cell.I am fetching values from a nsmutable dictionary and set text box text in the cell using following codes.

-(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;

}

In this situation my application is crashed show error ....

Terminating app due to uncaught exception '

NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance.

I find the reason for that one using below mentioned method.[datadictionary objectForKey:_currencyRateValueDetails[row]] object is is in nsnumber format .

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

so rewrite my code to

-(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;


}

This time application crash again show the error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString stringValue]: unrecognized selector sent to instance.

How can I solve this issue?

Upvotes: 0

Views: 78

Answers (1)

user2071152
user2071152

Reputation: 1195

You can use

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

Hope this helps.

Upvotes: 0

Related Questions