ios - Convert NSObject to NSNumber -


how convert object of type nsobject nsnumber in objective-c?

in android this:

if(value instanceof integer){    intvalue = (integer)value; } 

but how can convert value in objective-c?

my code:

-(void)changewithvalue:(nsobject*)value{      if([value iskindofclass:[nsnumber class]])           float floatvalue = [value floatvalue];  } 

but not working :(

help me please. thanks

after clarification of error objective-c-ese solution use id parameter type. type id means "any object type" , compiler allows call method. have code along lines of:

- (void)changewithvalue:(id)value {    if([value iskindofclass:[nsnumber class]])    {       float floatvalue = [value floatvalue];       ...    }    else    {       // handle not `nsnumber`    } } 

you can make more general testing method rather type using respondstoselector::

- (void)changewithvalue:(id)value {    if([value respondstoselector:@selector(floatvalue)])    {       float floatvalue = [value floatvalue];       ...    }    else    {       // handle case value not support floatvalue    } } 

hth


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -