Katya Katya - 4 months ago 53
Objective-C Question

Convert NSObject to NSNumber

How to convert object of type

NSObject
to
NSNumber
in Objective-C?

In Android I do this:

if(value instanceof Integer){
intValue = (Integer)value;
}


But how I can convert value in Objective-C?

My code:

-(void)changeWithValue:(NSObject*)value{
if([value isKindOfClass:[NSNumber class]])
float floatValue = [value floatValue];
}


But it is not working :(

Help me please. Thanks

Answer

Have you try this

-(void)changeWithValue:(NSObject*)value{
 if([value isKindOfClass:[NSNumber class]])
      NSNumber *num = (NSNumber*)value;
      float floatValue = [num floatValue];
 }