Adami
Adami

Reputation: 335

Obj-C How to call a method with return?

I have a method with a return value that I want to call from my MainviewController.m. Also, I must pass a value (float) to the method when calling it. But I'm having trouble with that, I tried to debug and add some breakpoints and NSLog in the method but it appears that the method is not being called, since the debugger doesn't stop at the breakpoint and doesn't print the NSLog. (the final print (calculatorScreen.text...) just print (null))

MainViewController.m

currentNumber = currentNumber *10 + (float)[sender tag];
NSNumber *convertedNumber = [[NSNumber alloc] init];
NSString *nf = [convertedNumber customFormatNumber:currentNumber];
calculatorScreen.text = [NSString stringWithFormat:@"%@",nf]; // it's printing (null) :(

NSNumber+FormatNumber.h

@interface NSNumber (FormatNumber)
-(NSString *) customFormatNumber:(float)n1;

NSNumber+FormatNumber.m

-(NSString *) customFormatNumber:(float)n1
{
    NSLog(@" %f" ,n1); // for debug and a breakpoint here
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setMaximumFractionDigits:2];
        [formatter setMinimumFractionDigits:0];
        NSString *nf = [formatter stringFromNumber:[NSNumber numberWithFloat:n1]];
    NSLog(@"Class %@" ,nf); // for debug
    return nf;
}

What am I missing here?

Upvotes: 0

Views: 108

Answers (2)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 22006

[[NSNumber alloc]init] returns nil because you aren't specifying the number value, and nil targeted actions are ignored.

Let me tell you that you are following a long and unnecessary way, I would just write all this way:

NSNumberFormatter* formatter=[NSNumberFormatter new];
[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:0];
formatter.numberStyle= NSNumberFormatterDecimalStyle;
calculatorScreen.text= [formatter stringFromNumber: @(currentNumber*10.0+[sender tag]) ];

Upvotes: 5

zurbergram
zurbergram

Reputation: 429

Are you trying to overwrite NSNumber?

For CustomNumber.h

@interface CustomNumber:NSNumber
-(NSString *) customFormatNumber:(float) n1;

For CustomNumber.m

-(NSString *) customFormatNumber:(float)n1
{
    NSLog(@" %f" ,n1); // for debug and a breakpoint here
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setMaximumFractionDigits:2];
    [formatter setMinimumFractionDigits:0];
    NSString *nf = [formatter stringFromNumber:[NSNumber numberWithFloat:n1]];
    NSLog(@"Class %@" ,nf); // for debug
    [formatter release];
    return nf;
}

Then in MainViewController.m

currentNumber = currentNumber *10 + (float)[sender tag];
CustomNumber *convertedNumber = [[CustomNumber alloc] init];
calculatorScreen.text = [convertedNumber customFormatNumber:currentNumber];

OR

In MainViewController.m create the method and then the code will be

currentNumber = currentNumber *10 + (float)[sender tag];
calculatorScreen.text = [self customFormatNumber:currentNumber];

Upvotes: 0

Related Questions