Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Introduction to Objective-C Syntax Overview

Andrew Nguyen
Andrew Nguyen
2,470 Points

Stringing parameters for a method?

Hi, I got a bit confused about this.

In an OOP language such as java a method would be written as:

public int addition(int a, int b){ etc..... }

However, in Objective-C the method would be written as:

-(int)addition:(int)a b:(int)b;

My question is, for the second param b:(int)b , what is the first b before the colon? Is it some place holder or something or are both b's just names? I am a bit confused because the first param is addition:(int)a and the addition is the method name but the first b isn't a method name.

Thanks.

2 Answers

-(int)addition:(int)a withB:(int)b;

Objective-C is considered a self documenting language, so the bit before the : is describing what the next argument is.

The real function name is a concatenation of the descriptors. "addition withB" in the case I've given

It's a weird concept to grasp at first but it makes coding easier imo.

Andrew Nguyen
Andrew Nguyen
2,470 Points

Ohh okay so the bit before the : is just for documentation purposes?

It's a descriptor really of what it's expecting, so documenting is one way to put it. Here are a couple of examples of how it would be used.

[MyObject initWithString:@"string" andArray:myArrayObject]; [MyObject initWithString:@"string" andDictionary:myDictionaryObject];

You'll get the idea of it as you move along in the track.