Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

EdChambo

macrumors newbie
Original poster
Nov 14, 2010
15
0
Hi guys i just started learning Objective-C and i just have a quick question.

heres the program:
Code:
#import <Foundation/Foundation.h>

@interface Fraction : NSObject
{
	int numerator;
	int denominator;
}

-(int) numerator;
-(int) denominator;
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

@implementation Fraction

-(int) numerator
{
	return numerator;
}

-(int) denominator
{
	return denominator;
}

-(void) print
{
	NSLog (@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n;
{
	numerator = n;
}

-(void) setDenominator: (int) d;
{
	denominator = d;
}

@end



int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	Fraction *myFraction = [Fraction new];

	[myFraction setNumerator: 1];
	[myFraction setDenominator: 3];
	
	NSLog (@"The value of myFraction is: %i/%i",
		   [myFraction numerator], [myFraction denominator]);
	[myFraction release];
	
    [pool drain];
    return 0;
}

(got that outa a txt book.)

and my question is: why does

Code:
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

have to have the arguments n and d?

couldnt you just put it as :
Code:
-(void) setNumerator;
-(void) setDenominator;
????

why does it need those arguments to set the fraction?
 
Last edited:
Do you know any other programing languages or is this your first? (It would help to answer the question if we know more about what is familiar to you).

B
 
why does it need those arguments to set the fraction?

Um, n and d can be anything you want them to be, look at this:
Code:
-(void) setDenominator: (int) d;
{
	denominator = d;
}

So that's why they are like that, however you could say denominator = pancakes and it'd still work as long as you declared it earlier. like this:
Code:
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) pancakes;
 
If you did that, you'd have to change the implementation to

Code:
-(void) setDenominator: (int) pancakes;
{
	denominator = pancakes;
}

No?

B
 
Um, n and d can be anything you want them to be, look at this:
Code:
-(void) setDenominator: (int) d;
{
	denominator = d;
}

So that's why they are like that, however you could say denominator = pancakes and it'd still work as long as you declared it earlier. like this:
Code:
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) pancakes;

my first language.

and i understand that n is just an argument and can be anything... but y do you need it atall y can u just say set denominator:3 y does there need to be the d in there? it just seems pointless?

cus ur making this d then just saying its = to the denominator.... y dont u just say setdenominator and it sets the denominator


yeh im a noob... thats y im here... so i can be not a noob :p

or pancakes... y do i need that... y cant it just change the denominator?
 
it just seems pointless?

It's not. It's a place holder that lets you do things like:

Code:
[aFraction setDenominator: 4];

It lets the computer know that it should expect an integer there and later what to do with it. (assign its value to the denominator of your fraction object).

So, the following code would not be correct because the number there is not an integer.
Code:
[aFraction setDenominator: 4.356];

and

Code:
[aFraction setDenominator];
also would not be correct since you didn't give an integer.

EDIT: Read the bit under "Method Arguments" in chapter 3 of Kochan's book since that seems to be what you are following.

B
 
Last edited:
EDIT: Read the bit under "Method Arguments" in chapter 3 of Kochan's book since that seems to be what you are following.

yeh :L its a good book. i was just confused over this cus it seems u have 2 denominator values 1 as denominator and 1 as d?

It's not. It's a place holder that lets you do things like:

Code:
[aFraction setDenominator: 4];

It lets the computer know that it should expect an integer there and later what to do with it. (assign its value to the denominator of your fraction object).

So, the following code would not be correct because the number there is not an integer.
Code:
[aFraction setDenominator: 4.356];

and

Code:
[aFraction setDenominator];
also would not be correct since you didn't give an integer.

EDIT: Read the bit under "Method Arguments" in chapter 3 of Kochan's book since that seems to be what you are following.

B

i get it :D:D:D:D... ima bit stupid :L

how long u rekon it takes before u can wright apps for the app store?
 
Last edited by a moderator:
Nope.

The following works fine:
Code:
@interface Fraction [I]...stuff left out here...[/I]
-(void) setDenominator: (int) pancakes;

@end

@implementation Fraction
[I]...stuff left out here...[/I]
-(void) setDenominator: (int) potrzebie;
{
	denominator = potrzebie;
}
There is no requirement that the parameter names in the @interface match the parameter names in the @implementation.

It will generally make reading the code easier if you don't give senseless names to things, and use a coherent naming approach, but the compiler won't care. It's just a symbol, and the only thing the compiler needs is for it to follow the rules for naming symbols.
 
Nope.

The following works fine: <snip>
There is no requirement that the parameter names in the @interface match the parameter names in the @implementation.

It will generally make reading the code easier if you don't give senseless names to things, and use a coherent naming approach, but the compiler won't care. It's just a symbol, and the only thing the compiler needs is for it to follow the rules for naming symbols.

Right, but I just like it that way for making the code easier to read.
 
There is no requirement that the parameter names in the @interface match the parameter names in the @implementation.

Sorry for the confusion. Perhaps a poor choice of words on my part. I wasn't meaning to imply that the @interface and @implementation needed to match, but that you had to be consistent within the @implementation.

i.e.
Code:
@implementation Fraction
-(void) setDenominator: (int) pancakes;
{
	denominator = potrzebie;
}
wouldn't work and I meant that you would need to change potzerbie to pancakes within the implementation.

How would you phrase that properly?

B
 
How would you phrase that properly?

I'm not sure there's a simple answer.

The issue is scope, parts, and consistency within the scope of a particular part. Maybe something like "each part must be self-consistent", but that leaves "part" pretty vague. "Each method must be self-consistent", but the principle applies to more than just methods.

"Each context" is also pretty vague, and hard to pin down because contexts are hierarchical: there is a context in which instance variables are "in scope", and that context continues across all the methods within an @implementation/@end grouping. There's also a "global context" that would encompass any extern variables, class declarations, etc.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.