I am trying to get NSNumber to respond to the initWithFloat method, but my program is responding with an “NSNumber may not respond to initWithFloat method” and is not returning the right result.
Here is the code:
#import "VariableTestAppDelegate.h"
@implementation VariableTestAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
NSNumber *myNumber;
myNumber = [NSNumber initWithFloat:3.47];
NSLog(@"The value in NSNumber is %@", myNumber);
}
@end
The error message which gets sent to the console reads:
2011-03-12 09:00:08.569 VariableTest[6228:a0f] +[NSNumber initWithFloat:]: unrecognized selector sent to class 0x7fff70bfd808
2011-03-12 09:00:08.572 VariableTest[6228:a0f] +[NSNumber initWithFloat:]: unrecognized selector sent to class 0x7fff70bfd808
What is the problem? TIA.
-initWithFloat:is not a class method. You need to create an instance first and then send it-initWithFloat:.Or you can use the convenience method
This second case gives you an
NSNumberthat you do not own. If you want to keep it around you need to retain it.