0

I can't figure out why variable number is not being assigned to array self.items. In the debugger it is showing value of NULL.

-(void)pushItem:(double)number{
    [self.items addObject:[[NSNumber alloc]initWithDouble:number]];

    if (self.items.lastObject == 0)
    {
        // ** need to add variable number to self.items array**
       
    }
}
4
  • 1
    You probably never initialized your items property. Also, your if check isn't doing what you probably think it is trying to do. The only way that will ever be true is if self.items is nil or if there are no objects in the array. Commented Sep 28, 2023 at 1:11
  • FYI - Easier syntax: [self.items addObject:@(number)];. Commented Sep 28, 2023 at 1:11
  • Did you initialize self.items? Self.items = [NSMutableArray new]; Additionally if you want to check for a number value you should call doubleValue or integerValue on the stored object Commented Sep 28, 2023 at 5:11
  • Is self.items nil? self.items.lastObject == 0 doesn't seems to be the correct way too. Did you meant if ([self.items.lastObject equalsTo:@(0.0)]) or if (self.items.lastObject == nil)? ... ? Commented Sep 28, 2023 at 7:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.