Ascorbin Ascorbin - 4 months ago 7x
Objective-C Question

Objective C method declaration/ calling

I'm doing a video tutorial on iPhone programming, it's a very simple calculator app. At one point I declare the following method::

- (NSString*)calculate:(NSString*)operation withNumber:(NSInteger)number
return nil;

It's not implemented yet at this point. Then I want to call the method with:

self.display.text = [self calculate:[sender currentTitle] withNumber:[self.display.text intValue]];

Xcode is giving me an error here: 'expected expression'.
What's wrong here? And what is
in the method? I would understand

- (NSString*)calculate :(NSString*)operation :(NSInteger)number;

Thats a method that takes a string and an int as parameters and returns a String. I don't get what
does here.


OK, for it to work, you will need to remove the unnecessary spaces :

- (NSString*)calculate:(NSString*)operation withNumber:(NSInteger)number{

and on calling the method too of course.

As to 'what is withNumber ? ' : this is the way multi-input method look like in Objective-C, the name of the method does not precede the arguments. The method is actually named calculate:withNumber: in the runtime system

I strongly recommend reading some beginner's guide

You could do - (NSString*)calculate:(NSString*)operation :(NSInteger)number and then you will have to call [self calculate:myString :myNumber]; but the vast majority of Objective-C user would not do that : the language gives you the opportunity to clarify your code and specify what arguments is what : take that opportunity.