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

RobertD63

macrumors 6502
Original poster
Feb 17, 2008
403
42
A place
Im reading a book about learning how to program in Objective-c, and its hard for me to just read and learn. I need to ask loads of questions. So here is my first that I have. The author gives this in the book:
[myWindow erase]; Clear the window
Now I get that myWindow is the variable and that erase is the method but how does the complier know that you want the window to clear?
 
The code [myWindow erase] clears a window for the same reason that turning the key in the ignition starts my car - because someone wired it up that way.

To be more specific, myWindow is an instance of some class*, right? Just like my car is an instance of the HyundaiBeater class. I say "an instance" because there are many other instances of HyundaiBeater, but only this one is mine.

The "erase" is the method you're calling on that instance. Well, someone at Apple wrote the window class and they wrote the "erase" method and wrote the code in that method to clear all of the pixels. They did the hard work so you didn't have it.

The opposite question to yours would be "When I write [myWindow turnBlue] why doesn't it turn blue?" And the answer is similar - because no one at Apple wrote a turnBlue method. Same reason nothing happens when I stick my key in the cig lighter - it's not designed that way. No worries though, you can write your own classes and methods to build on what's already there.

Does that help at all? If not, ask another question.

By the way, I can see what methods are available by looking at the documentation from Apple. The same documentation is available in XCode too.

http://developer.apple.com/iPhone/l...ef/doc/uid/TP40006817-CH3-DontLinkElementID_1

*For pedants: I know myWindow is not an instance, but it points to an instance, and let's not get into pointers yet.
 
Okay sorry for not replying for a little while but I've been studying. So still on the same topic I've made this program that displays fractions in NSLog. Here is the code:
Code:
#import <Foundation/Foundation.h>

@interface Fraction : NSObject
{
	int num;
	int dem;
	int num1;
	int dem1;
}

-(void) print;
-(void) display;
-(void) setNum: (int) n;
-(void) setDem: (int) d;
-(void) setNum1: (int) n1;
-(void) setDem1: (int) d1;


@end

@implementation Fraction: NSObject

-(void) print
{
	NSLog(@"%i/%i", num, dem);
}
-(void) display
{
	NSLog (@"%i/%i", num1, dem1);
}

-(void) setNum: (int) n
{
	num = n;
}
-(void) setDem: (int) d;
{
	dem = d;
}

-(void) setNum1: (int) n1;
{
	num1 = n1;
}

-(void) setDem1: (int) d1;
{
	dem1 = d1;
}

@end



int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	Fraction *myFraction;
	Fraction *notMyFraction;
	
	myFraction =[[Fraction alloc] init];
	notMyFraction =[[Fraction alloc] init];
	
	[myFraction setNum: 1];
	[myFraction setDem: 3];
	
	NSLog (@"The fraction is:");
	[myFraction print];
	
	[notMyFraction setNum: 2];
	[notMyFraction setDem: 3];
	NSLog (@"The fraction that isnt yours is:");
	[notMyFraction display];
	[myFraction release];
	[notMyFraction release];
	[pool drain];
	return 0;
}

First off I'd like to say sorry for the lack of commenting but I was just playing around trying little things out. I wanna ask though, for the [notMyFraction display]; I cant seem to get a fraction out of that. The output for the program is
The fraction is: 1/3
The fraction that isnt yours is: 0/0
I cant seem to figure out why? Any suggestions?
 
What's the point in num1 and dem1, you don't seem to use them?

Code:
[notMyFraction setNum: 2];
[notMyFraction setDem: 3];

Should that bit above not be:

Code:
[notMyFraction setNum[B]1[/B]: 2];
[notMyFraction setDem[B]1[/B]: 3];
 
Your problem is plain and simple. When you call [notMyFraction display]; you're displaying num1 and dem1, but you only set values for num and dem. You could either set values for num1 and dem1 or call [notMyFraction print];
 
Ah see I have money on that if I commented it better I would have cought that. I see where I messed up, I didnt have:
[notMyFraction setNum1: 2];
[notMyFraction setDem1: 3];
I really had this in the code:
[notMyFraction setNum: 2];
[notMyFraction setDem: 3];

Thank you guys so much for all the replies. I was actually surprised to see all of the replies. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.