Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7728163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:38:57+00:00 2026-06-01T05:38:57+00:00

I want to access the property storedValue from the class method popOperandOffStack: , but

  • 0

I want to access the property storedValue from the class method popOperandOffStack:, but I get an error. I read that you cannot access storedValue because it is a class method or something like that. Can anyone tell me how can I access storedValue or how can I make storedValue global so that I can use it in popOperandOffStack:?

Here is my header:

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

- (void)pushOperand:(double)operand; // - = instance method
- (double)performOperation: (NSString *)operation;

@property (nonatomic,readonly) id program;

+ (double)runProgram:(id)program; // + = class method
+ (NSString *)descriptionOfProgram:(id)program;
@end

And this is my m file:

#import "CalculatorBrain.h"

@interface CalculatorBrain()
@property (nonatomic,strong) NSMutableArray *programStack;
@property (nonatomic) double storedValue;
@end

@implementation CalculatorBrain

@synthesize programStack = _programStack;
@synthesize storedValue = _storedValue;

- (NSMutableArray *)programStack
{
    if (_programStack == nil)
        _programStack = [[NSMutableArray alloc] init];

    return _programStack;
}

- (void)setOperandStack:(NSMutableArray *)programStack
{
    _programStack = programStack;
}

- (void)pushOperand:(double)operand
{
    [self.programStack addObject:[NSNumber numberWithDouble:operand]];
}

- (double)performOperation: (NSString *)operation
{
    [self.programStack addObject:operation];
    return [[self class] runProgram:self.program];
}

- (id)program 
{
    return [self.programStack copy];
}

+ (NSString *)descriptionOfProgram:(id)program
{
    return @"Implement this in Assignment 2";
}

+ (double) popOperandOffStack:(NSMutableArray *)stack
{
    double result = 0;

    id topOfStack = [stack lastObject];
    if (topOfStack) 
        [stack removeLastObject];

    if ([topOfStack isKindOfClass:[NSNumber class]])
    {
        result = [topOfStack doubleValue];
    } 
    else if ([topOfStack isKindOfClass:[NSString class]]) 
    {
        NSString *operation = topOfStack;
        if ([operation isEqualToString:@"+"]){
            result = [self popOperandOffStack:stack] + [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"-"]){
            result = -[self popOperandOffStack:stack] + [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"*"]){
            result = [self popOperandOffStack:stack] * [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"/"]){
            double divide = [self popOperandOffStack:stack];
            if(divide){
                result = [self popOperandOffStack:stack] / divide;
            } else {
                result = 0;
            }
        } else if ([operation isEqualToString:@"1/x"]){
            result = 1 / [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"sqrt"]){
            double sqrtNumber = [self popOperandOffStack:stack];
            if (sqrtNumber > 0){
                result = sqrt(sqrtNumber);
            } else {
                result = 0;
            }
        } else if ([operation isEqualToString:@"sin"]){
            result = sin( [self popOperandOffStack:stack]);
        } else if ([operation isEqualToString:@"cos"]){
            result = cos( [self popOperandOffStack:stack]);
        } else if ([operation  isEqualToString:@"MS"]){
            self.storedValue = [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"MR"]){
            result = self.storedValue; //error here
        } else if ([operation isEqualToString:@"MC"]){
            result = [self popOperandOffStack:stack];
            self.storedValue = 0; //error here
        } else if ([operation isEqualToString:@"M+"]){
            result = [self popOperandOffStack:stack];
            self.storedValue = self.storedValue + result; //error here
        } else if ([operation isEqualToString:@"M-"]){
            result = [self popOperandOffStack:stack];
            self.storedValue = self.storedValue - result; //error here
        } else if ([operation isEqualToString:@"C"]){
            result = 0;
            while ( [self popOperandOffStack:stack] )
            {
                //clear operands until returns NO
            }
        } else if ([operation isEqualToString:@"CE"]){
            result = 0;
            self.storedValue = 0; //error here
            while ( [self popOperandOffStack:stack] )
            {
                //clear operands until returns NO
            }
        }   

        if (![operation isEqualToString:@"MS"] && ![operation isEqualToString:@"CE"])
        {
            return result;
        } else {
            return self.storedValue;
        }
    }
    return result;
}

+ (double)runProgram:(id)program
{
    NSMutableArray *stack;
    if ([program isKindOfClass:[NSArray class]]) {
        stack = [program mutableCopy];
    }
    return [self popOperandOffStack:stack];
 }

@end

I cannot use self.storedValue. What should I do besides changing the + (static method) to - (instance method) (I am not allowed to do that)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:38:58+00:00Added an answer on June 1, 2026 at 5:38 am

    You can’t use instance getter/setter methods from inside a class method because you don’t have an object that is an instance of CalculatorBrain. (I haven’t used that course but I would hope that it explains the difference between classes and objects.)

    Even though it’s not a good idea from the point of view of creating an object-oriented program, you could use a static variable to hold your double. Look at a C language reference for the phrase “external static” to see examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Private property that I want to access in my MS Test
I can define a class with a property to access my ivars from outside
Get error 'ClassLibrary3.Class1.a' is a 'property' but is used like a 'type' when i
I want to be able to access a property or member of a class
i have bean that contain method [void return] and want access to this bean
I want to access a MySQL database directly from JavaScript code in an HTML
I want to access a Session value in jquery method in the ASP.NET MVC
I want to access the raw HTML code that my ASP.NET System.Web.UI.Page is about
This is pretty rudimentary, but I want to know how I can access a
I have some instance variables in my class that I'd want to be accessible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.