2

I need some help. I am new to Objective-C and trying to learn how to program iPhone Applications in Xcode 3.0. I am trying to pass my array through a function and am confused. Thank you for your help. Here is my code:

#import <Foundation/Foundation.h>

void add(int x) {
    NSLog(@"%i + 2 = %i", x, x + 2);
}


int main (int argc, const char * argv[]) {

 NSNumber *arrayNumber = [[NSNumber alloc] initWithInt:5];
 NSNumber *arrayNumber2 = [[NSNumber alloc] initWithInt:9];

 NSMutableArray *arrayNumbers = [[NSMutableArray alloc] initWithCapacity:1];

 [arrayNumbers addObject:arrayNumber];
 [arrayNumbers addObject:arrayNumber2];



 NSLog(@"object at index 0 = %i", [[arrayNumbers objectAtIndex:0] intValue]);
 NSLog(@"object at index 1 = %i", [[arrayNumbers objectAtIndex:1] intValue]);



for(NSNumber *answer in arrayNumbers) {
    add();
}


return 0;
}

I am confused by what to put in the parentheses in the part with add():

for(NSNumber *answer in arrayNumbers) {
    add();
}

1 Answer 1

1

You need to unbox the NSNumber using -intValue like this:

for(NSNumber *answer in arrayNumbers) {
    add([answer intValue]);
}
Sign up to request clarification or add additional context in comments.

Comments

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.