-2

If you take this method call for instance(from other post)

- (int)methodName:(int)arg1 withArg2:(int)arg2
{
    // Do something crazy!
    return someInt;
}

Is withArg2 actually ever used for anything inside this method ?

0

3 Answers 3

5

withArg2 is part of the method name (it is usually written without arguments as methodName:withArg2: if you want to refer to the method in the documentation), so no, it is not used for anything inside the method.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, I mean if your not going to use it anywhere why have it in the first place then.
Think about it as a "name" for the second argument so you always know what to put there. (Pretty helpful if you have a long parameter list!). For a longer explanation, see this page and feel free to disagree: cocoawithlove.com/2009/06/method-names-in-objective-c.html
Thank you, that is really useful, I'm just getting started
2

As Tamás points out, withArg2 is part of the method name. If you write a function with the exact same name in C, it will look like this:

int methodNamewithArg2(int arg1, int arg2)
{
  // Do something crazy!
  return someInt;
}

Coming from other programming languages, the Objective-C syntax at first might appear weird, but after a while you will start to understand how it makes your whole code more expressive. If you see the following C++ function call:

anObject.subString("foobar", 2, 3, true);

and compare it to a similar Objective-C method invocation

[anObject subString:"foobar" startingAtCharacter:2 numberOfCharacters:3 makeResultUpperCase:YES];

it should become clear what I mean. The example may be contrived, but the point is to show that embedding the meaning of the next parameter into the method name allows to write very readable code. Even if you choose horrible variable names or use literals (as in the example above), you will still be able to make sense of the code without having to look up the method documentation.

Comments

0

You would call this method as follows:

int i=[self methodName:arg1 withArg2:arg2];

This is just iOs's way of making the code easier to read.

1 Comment

Technically it has nothing to do with iOS and everything to do with Objective-C and its naming conventions.

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.