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
Post a Comment