Instances and methods

Discussion in 'iOS Programming' started by RobertD63, Jan 31, 2010.

  1. RobertD63 macrumors 6502

    Joined:
    Feb 17, 2008
    Location:
    A place
    #1
    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:
    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?
     
  2. xsmasher macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #2
    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.
     
  3. RobertD63 thread starter macrumors 6502

    Joined:
    Feb 17, 2008
    Location:
    A place
    #3
    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
    I cant seem to figure out why? Any suggestions?
     
  4. ethical macrumors 68000

    Joined:
    Dec 22, 2007
    #4
    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];
    
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Look at this closer. You sure you want to be calling setNum: and setDem: in this case?
     
  6. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #6
    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];
     
  7. RobertD63 thread starter macrumors 6502

    Joined:
    Feb 17, 2008
    Location:
    A place
    #7
    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. :)
     

Share This Page