0 Replies - 1436 Views - Last Post: 30 July 2011 - 09:48 PM

#1 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Unrecognized selector sent to instance

Posted 30 July 2011 - 09:48 PM

On line 45 in my view controller I get the error -[CalculatorBrain descriptionOfExpression:]: unrecognized selector sent to instance. Does anybody have any ideas of why this happens.

View Controller
#import "CalculatorViewController.h"

#define VARIABLE_PREFIX @"%"

@implementation CalculatorViewController


- (CalculatorBrain *)brain
{
    if(!brain) brain= [[CalculatorBrain alloc]init];
    return brain;
}

- (IBAction) digitPressed:(UIButton *)sender
{
    NSString *digit= [[sender titleLabel]text];
    
    if(userIsInTheMiddleOfTypingANumber)
    {
        [display setText:[[display text] stringByAppendingString:digit]];
    } else
    {
        [display setText:digit];
        userIsInTheMiddleOfTypingANumber= YES;
    }
}

- (IBAction) operationPressed:(UIButton *)sender
{
    if([sender.titleLabel isEqual:@"Clr"]) {
        display.text= 0;
    }
    
    if(userIsInTheMiddleOfTypingANumber) {
        NSNumber *operand= [[NSNumber alloc]initWithDouble:[[display text] doubleValue]];
        [[self brain] setOperand:[[display text] doubleValue]];
        [[self brain].internalExpression addObject:operand];
        userIsInTheMiddleOfTypingANumber= NO;
        [operand release];
    }
    
    NSString *operation= [[sender titleLabel]text];
    //double result= [[self brain] performOperation:operation];
    [[self brain].internalExpression addObject:operation];
    [display setText:[[self brain] descriptionOfExpression:[self brain].expression]];
    //[display setText:[NSString stringWithFormat:@"%g", result]];
}

- (IBAction) setVariableAsOperand:(UIButton *)sender
{
    [[self brain].internalExpression addObject:[VARIABLE_PREFIX stringByAppendingString:[[sender titleLabel]text]]];
}

- (IBAction) evaluateExpression:(UIButton *)sender
{
    NSNumber *X= [[NSNumber alloc]initWithInt:4];
    NSNumber *Q= [[NSNumber alloc]initWithInt:6];
    NSNumber *Z= [[NSNumber alloc]initWithInt:8];
    
    NSDictionary *variables= [[NSDictionary alloc] initWithObjectsAndKeys:
                              X, @"%x", Q, @"%q", Z, @"%z", nil];
    
    [[self brain] evaluateExpression:[self brain].expression usingVariableValues:variables];
    
    [X, Q, Z release];
}

@end



Model
//
//  CalculatorBrain.m
//  Calculator
//
//  Created by Greg Sapienza on 7/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CalculatorBrain.h"


@implementation CalculatorBrain
@synthesize expression, operand;
@synthesize internalExpression;

- (double)setOperand:(double)aDouble
{
    operand= aDouble;
    return operand;
}


- (id)expression
{
    return internalExpression;
}

- (void)performWaitingOperand {
    if ([@"+" isEqual:waitingOperation])
    {
        operand+= waitingOperand;
    } else if ([@"*" isEqual:waitingOperation])
    {
        operand*= waitingOperand;
    } else if ([@"-" isEqual:waitingOperation])
    {
        operand= waitingOperand-operand;
    } else if ([@"/" isEqual:waitingOperation])
    {
        operand= waitingOperand/operand;
    }
}

- (double) performOperation:(NSString *)operation
{
    if ([operation isEqual:@"Sqrt"]) {
        operand= sqrt(operand);
    } else if([operation isEqual:@"Clr"]) {
            operand= 0;
            waitingOperand= 0;
    } else {
        [self performWaitingOperand];
        waitingOperation= operation;
        waitingOperand= operand;
    }
    
    return operand;
}

+ (double)evaluateExpression:(id)anExpression
usingVariableValues:(NSDictionary *)variables
{
    CalculatorBrain *brain= [[CalculatorBrain alloc]init];
    
    for (int i=0; [anExpression count]>i; i++)
    {
        if ([[anExpression objectAtIndex:i] isKindOfClass:[NSNumber class]])
            [brain setOperand:[[anExpression objectAtIndex:i]doubleValue]];
        
        if ([[anExpression objectAtIndex:i] isKindOfClass:[NSString class]])
            [brain performOperation:[anExpression objectAtIndex:i]];
        
        if ([[anExpression objectAtIndex:i] isKindOfClass:[NSString class]] && [[anExpression objectAtIndex:i] hasPrefix:@"%"])
        {
            NSNumber *number= [[NSNumber alloc]init];
            number= [variables objectForKey:[anExpression objectAtIndex:i]];
            [brain setOperand:[number doubleValue]];
            [number release];
        }
    }
    return [brain operand];
}

+ (NSString *)descriptionOfExpression:(id)anExpression
{
    NSString *theExpression= [[NSString alloc]init];
    NSString *temp= [[NSString alloc]init];
    for (int i=0; [anExpression count]>i; i++)
    {
        if([[anExpression objectAtIndex:i] hasPrefix:@"%"]) 
        {
            temp=[[anExpression objectAtIndex:i] substringFromIndex:1];
            [theExpression stringByAppendingString:temp];
        } else {
        [theExpression stringByAppendingString:[anExpression objectAtIndex:i]];
        }
    }
    [temp release];
    return theExpression;
}

+ (NSSet *)variablesInExpression:(id)anExpression
{
    NSSet *variables= [[NSSet alloc]init];
    NSString *temp= [[NSString alloc]init];
    for (int i=0; [anExpression count]>i; i++)
    {
        if([[anExpression objectAtIndex:i] hasPrefix:@"%"]) 
        {
            temp=[[anExpression objectAtIndex:i] substringFromIndex:1];
            [variables setByAddingObject: temp];
        }
    }
    [temp release];
    return variables;
}
@end



im not sure why all of these <b></b> appeared in the post but they are not part of the code.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1