Decimal on calculator app implementation method

| | August 11, 2015

I am working on adding to my currently functioning app by adding a much needed decimal option to my standard calculator. I have struggled finding a way to do this. I finally came across a way to implement a decimal today. The problem is that I am getting errors so I can’t build it to see if it works. Can somebody please tell me how to fix the error I’m having? Or a better way to implement a decimal?

Here is my code:

.h file:

//
//  ViewController.h
//  CalculatorPlus
//
//  Created by William Harris on 12/20/14.
//  Copyright (c) 2014 William Harris. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <StoreKit/StoreKit.h>
#import "Calculator.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"

@class ViewController;

@interface ViewController : UIViewController <UIActionSheetDelegate,   ADBannerViewDelegate> {

float result;
IBOutlet UILabel *calculatorScreen;
NSInteger currentOperation;
float currentNumber;

}

@property (strong, nonatomic) IBOutlet UILabel *calculatorScreen;
@property (strong, nonatomic) IBOutlet ADBannerView *ad1;
-(IBAction)buttonDigitPressed:(id)sender;
-(IBAction)buttonOperationPressed:(id)sender;
-(IBAction)cancelInput:(id)sender;
-(IBAction)cancelOperation:(id)sender;
-(IBAction)changeCalculatorTapped:(id)sender;

@end

.m file:

//
//  ViewController.m
//  CalculatorPlus
//
//  Created by William Harris on 12/20/14.
//  Copyright (c) 2014 William Harris. All rights reserved.
//

#import "ViewController.h"

NSUInteger decimalPlacesLimit = 2;

@interface ViewController ()

@end

@implementation ViewController

@synthesize ad1, calculatorScreen;

-(IBAction)buttonDigitPressed:(id)sender {

currentNumber = currentNumber *10 + (float) [sender tag];
calculatorScreen.text = [NSString stringWithFormat:@"%1.2f",currentNumber];
NSRange range = [self.calculatorScreen.text rangeOfString:@"."];

BOOL canUpdateScreen = YES;
if(range.location != NSNotFound) {
    if([sender tag] == 99) {
        // Already got dot, cannot show another dot
        canUpdateScreen = NO;
    } else {
        NSArray *explodedString = (NSArray*)[self.calculatorScreen.text     componentsSeparatedByString:@"."];
        if(explodedString[1].length >= decimalPlacesLimit) {
            canUpdateScreen = NO;
        }
    }
}

if(canUpdateScreen) {
    if([sender tag] == 99) {
        self.calculatorScreen.text = [NSString stringWithFormat:@"%@%@", self.calculatorScreen.text, @"."];
    } else {
        self.calculatorScreen.text = [NSString stringWithFormat:@"%@%ld", self.calculatorScreen.text, (long)[sender tag]];
    }
}
}

-(IBAction)buttonOperationPressed:(id)sender {

if (currentOperation == 0) result = currentNumber;
else {

    switch (currentOperation) {
        case 1:
            result = result + currentNumber;
            break;
        case 2:
            result = result - currentNumber;
            break;
        case 3:
            result = result * currentNumber;
            break;
        case 4:
            result = result / currentNumber;
            break;
        case 5:
            currentOperation = 0;
            break;
    }
}
currentNumber = 0;
calculatorScreen.text = [NSString stringWithFormat:@"%1.2f",result];
if ([sender tag] == 0) result = 0;
currentOperation = [sender tag];
}
-(IBAction)cancelInput:(id)sender {

currentNumber = 0;
calculatorScreen.text = @"0";

}

-(IBAction)cancelOperation:(id)sender {

currentNumber = 0;
calculatorScreen.text = @"0";
currentOperation = 0;
}

- (void)viewDidLoad {
[super viewDidLoad];

calculatorScreen.adjustsFontSizeToFitWidth = true;
}

-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

Error message I’m getting:

Error Message I'm Recieving

Can somebody please tell me how I can fix the error? Or, if there is a better way to implement decimals? I really need help getting the decimal working. Thank you!

2 Responses to “Decimal on calculator app implementation method”

  1. The objects in an NSArray are of type id (anonymous object.) You have to cast them to the desired type, or use bracket notation.

    You could rewrite the offending line as

        if (((NSString*)explodedString[1]).length >= decimalPlacesLimit) 
    

    or

        if ([explodedString[1] length] >= decimalPlacesLimit) 
    
  2. explodedString is a NSArray *. NSArrays store their objects as ids, which are basically just generic objects. Although you and I know that the contents of explodedString are NSString *s, the compiler doesn’t. It takes the safe route of keeping all of the objects in the array as ids.

    To summarize:
    The compiler doesn’t know that explodedString[1] is a string.

    Fix: Explicitly cast explodedString[1] to NSString *.

    if(((NSString *) explodedString[1]).length >= decimalPlacesLimit)
    //      ^
    //   Add this
    

Leave a Reply