1 2 Previous Next 15 Replies Latest reply: Jun 25, 2012 5:04 PM by TheNavigator
TheNavigator Level 1 Level 1 (0 points)

[internalExpression addObject:[NSNumber numberWithDouble:operand]];

 

[internalExpression addObject:[NSString stringWithString:operation]];

 

None of these work while I'm sure that both operation and operand have something in there. I've debugged it and saw that internalExpression just points to 0x0, which is obviously nil. Do I need to initialize it? Although I use NSString without initialization. Both are instance variables by the way.


Xcode, Mac OS X (10.6.7), Xcode 4.0.1
  • 1. Re: Program received signal: "EXC_BAD_ACCESS"
    TheNavigator Level 1 Level 1 (0 points)

    Initializing it fixed the 0x0 thing, but I still can't solve the main issue when this method is called. The error received is the title of the discussion.

     

    - (IBAction)evaluatePressed

    {

        [display setText:[NSString stringWithFormat: @"%d", [CalculatorBrain evaluateExpression:[[self brain].expression copy]

                                                                  usingVariableValues:[[NSDictionary alloc] initWithObjectsAndKeys:@"x", 2, @"a", 4, @"b", 8, nil]] ]];

    }

     

    Any help is appreciated. Thanks in advance.

  • 2. Re: Program received signal: "EXC_BAD_ACCESS"
    xnav Level 5 Level 5 (6,630 points)

    @"x", 2, @"a", 4, @"b", 8, nil


    For one thing 2,4, and 8 are not valid keys.

  • 3. Re: Program received signal: "EXC_BAD_ACCESS"
    etresoft Level 7 Level 7 (24,265 points)

    Anything inside an "NS" container must be an object. If you want to store a number, store an NSNumber.

  • 4. Re: Program received signal: "EXC_BAD_ACCESS"
    TheNavigator Level 1 Level 1 (0 points)

    Much thanks guys for your help

     

    I'm stuck at another problem though.

     

    NSMutableArray *evaluatingExpression = [anExpression copy];

       

        if ( ! [[evaluatingExpression lastObject] isEqual:@"="] ) {

            [evaluatingExpression addObject:@"="];

        }

     

    It crashes at [evaluatingExpression addObject:@"="]; saying "Program received signal: SIGART".

     

    If this case is wrong, it crashes at another point.

     

                        for (NSString *variableObject in variables) {

                            if ([variableObject isEqualToString:character]) {

                               

                                [workerBee setOperand: [[variables objectForKey:variableObject] doubleValue]];

                                break;

                            }

                        }

     

    It crashes at if ([variableObject isEqualToString:character]) saying the same message.

     

    Debug logs statement I noticed:

     

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x540cc00'

     

    How NSDictionary is declared:

     

    - (IBAction)evaluatePressed

    {

        [display setText:[NSString stringWithFormat: @"%g", [CalculatorBrain evaluateExpression:[[self brain].expression copy]

                                                                  usingVariableValues:[[NSDictionary alloc]

                                                                                        initWithObjectsAndKeys:

                                                                                        @"x", [NSNumber numberWithDouble:2],

                                                                                        @"a", [NSNumber numberWithDouble:4],

                                                                                        @"b", [NSNumber numberWithDouble:8], nil]] ]];

    }

     

    I'm pulling out my hair because of those errors..

     

    Any help is appreciated, and thanks for those who helped me at the first error again.

  • 5. Re: Program received signal: "EXC_BAD_ACCESS"
    etresoft Level 7 Level 7 (24,265 points)

    NSNumber doesn't respond to isEqualToString:. Keep values in an NSString until you are sure they are a valid number. This was true since the early days of C. Otherwise, an invalid conversion will just return 0, a valid number.

  • 6. Re: Program received signal: "EXC_BAD_ACCESS"
    TheNavigator Level 1 Level 1 (0 points)

    But I'm not comparing an NSNumber here. Isn't variable object supposed to be a string? @"x" or @"a" or @"b" ?

     

    character is an NSString by the way.

  • 7. Re: Program received signal: "EXC_BAD_ACCESS"
    etresoft Level 7 Level 7 (24,265 points)

    You may have defined your fast enumeration variable as an NSString, but apparently the objects inside are NSNumbers. In Objective-C, the (NSWhatever *) is just syntactic sugar to help you read the program. Internally, everything is an id.

  • 8. Re: Program received signal: "EXC_BAD_ACCESS"
    TheNavigator Level 1 Level 1 (0 points)

    Yes I know about the id thing, but I still don't know how to enumerate through these objects.

     

    I asked a professional friend who recommended sending all keys to the dictionary and enumerating through them, then sending objectForKey to the dictionary to get the object.

     

    I know that this will mostly work, but why doesn't what I wrote work?

  • 9. Re: Program received signal: "EXC_BAD_ACCESS"
    etresoft Level 7 Level 7 (24,265 points)

    You appear to be using NSNumbers as keys in your dictionary. Technically there isn't anything wrong with that. But if you enumerate over those keys you must use the correct type.

  • 10. Re: Program received signal: "EXC_BAD_ACCESS"
    TheNavigator Level 1 Level 1 (0 points)

    Just noticed that I flipped the order of keys and objects. I flipped them and everything worked fine.

     

    if ( ! [[evaluatingExpression lastObject] isEqual:@"="] ) {

            [evaluatingExpression addObject:@"="];

        }

     

    doesn't work though

     

    2012-06-24 05:14:57.754 Calculator[1602:207] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x54017d0

    2012-06-24 05:14:57.899 Calculator[1602:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x54017d0'

     

    evaluatingExpression is a copy of expression sent to the evaluateExpression method as an argument/parameter

     

    Shall I use [NSString stringWithString:@"="] instead?

     

    I can do it in an alter way getting out of adding an object way, but the main goal of solving these stuff is to know how to pass those code errors, so I need to know why it happens so if it happened one day where I must use it, I can solve it :|

     

    Thanks again etresoft for your help

     

    EDIT: Just tried [evaluatingExpression addObject:[NSString stringWithString:@"="] instead. Didn't work. Still the same error. I knew that there's something wrong with the method itself or evaluatingExpression object, but I just tried...

     

    Message was edited by: TheNavigator - Added the Edit

  • 11. Re: Program received signal: "EXC_BAD_ACCESS"
    etresoft Level 7 Level 7 (24,265 points)

    You can only send the addObject: message to an NSMutableArray

  • 12. Re: Program received signal: "EXC_BAD_ACCESS"
    TheNavigator Level 1 Level 1 (0 points)

    + (double)evaluateExpression:(id)anExpression

             usingVariableValues:(NSDictionary *)variables

    {

        NSMutableArray *evaluatingExpression = [anExpression copy];

       

        if ( ! [[evaluatingExpression lastObject] isEqual:@"="] ) {

            [evaluatingExpression addObject:[NSString stringWithString:@"="] ];

        }

  • 13. Re: Program received signal: "EXC_BAD_ACCESS"
    etresoft Level 7 Level 7 (24,265 points)

    Assuming anExpression really is an NSMutableArray, you may be able to do that. "mutableCopy" might be a better idea. Offhand I don't know what you get if you do a "copy" on a mutable object.

     

    Also, you don't need to create a new string with the @"=" literal. You can just use it.

  • 14. Re: Program received signal: "EXC_BAD_ACCESS"
    TheNavigator Level 1 Level 1 (0 points)

    Well, I tried using anExpression directly but I failed. I'll try that mutableCopy thing though.

1 2 Previous Next