0

For all intents and purposes, an Objective-C method declaration is simply a C function that prepends two additional parameters (see “Messaging” in the Objective-C Runtime Programming Guide ). Thus, the structure of an Objective-C method declaration differs from the structure of a method that uses named or keyword parameters in a language like Python, as the following Python example illustrates: In this Python example, Thing and NeatMode might be omitted or might have different values when called.

def func(a, b, NeatMode=SuperNeat, Thing=DefaultThing):
    pass

What's the goal of showing this example on an Objective-c related book?

1
  • Source Commented Jun 6, 2012 at 11:36

3 Answers 3

1

This is a (poor) example of how Objective-C does not support certain features that other languages, (for example, Python) may. The text explains that while Objective-C has "named parameters" of the format

- (void)myMethodWithArgument:(NSObject *)argument andArgument:(NSObject *)another;

Those parameters do not support defaults values, which Python does.

The mention of prepending two arguments hints at how message passing in Objective-C works under the hood, which is by prepending each method with a receiver object and a selector. You don't need to know this detail in order to write code in Objective-C, especially at a beginner level, but Apple explains this process here.

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

1 Comment

jinkies! so thats what a mystery is :) ....... now i got that those prepend params are for demo to show that obj-c don't work that way .. thankssssssss a lot for lovely reply. :)
0
def func(a, b, NeatMode=SuperNeat, Thing=DefaultThing):
    pass

NeatMode, Thing are optional named parameters in objective c they would be

- (void) func:(int)a :(int)b NeatMode:(object*)SuperNeat Thing:(object*)DefaultThing

Pleas read more about this subject http://www.diveintopython.net/power_of_introspection/optional_arguments.html

4 Comments

Please note that Objective-C does not have optional arguments.
no... i still didn't get it what you and this doc is talking about... :(
and doc also says that ..... Important The sub parts of an Objective-Cselectorname are not optional,nor can their order bevaried. In some languages, the terms “named parameters” and “keyword parameters” carry the implications that the parameters can vary at runtime, can have default values, can be in a different order, and can possibly have additional named parameters. None of these characteristics about parameters are true for Objective-C.
0

I think the point here is to differentiate between how you are "used" to receive parameters in functions and how objective-c does. Normally:

public void accumulate(double value, double value1) {                                    

}

And in objective-c:

-(void)accumulateDouble:(double)aDouble withAnotherDouble:(double)anotherDouble{


}

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.