L2P: the “Parts” of a Program - Returns, Accessor Methods, and Synthesizing

So far the PrintCount class methods we have created do not send anything back to the code that calls it. What if you want to get something back from a method you call? Well that would be a “Return” in programming parlance. This is yet another Wall-O-Text post that I'm going to hide behind a cut.

We are going to expand on PrintCount with a new method that returns the value of our numOfPrints variable. Strangely enough, we will call this method numOfPrints due to the coding conventions we will talk about later in this post.

So in the code, we would need to add the following to PrintCount.h:

  1. -(int) numOfPrints

And in our PrintCount.m:

  1. -(int) numOfPrints;
  2. {
  3. return numOfPrints;
  4. }

Pretty simple eh? All we are doing in the code is returning the integer of numOfPrints. We could return just about anything if we wanted, such as 1 or 2 as a status, or even other objects. So in our code now, we can get the current value of numOfPrints by simply calling [instanceName numOfPrints].
Here is an example, modifying our original HelloOOP.m program:

  1. PrintCount * phraseA = [[PrintCount alloc]init];
  2. [phraseA setText: @"This is Phrase A, Hello!"];
  3. [phraseA printText];
  4. [phraseA printText];
  5. [phraseA printNum];
  6. NSLog(@"Return from numOfPrints: %i",[phraseA numOfPrints]);

And here is the output:
2011-03-21 16:18:54.713 HelloOOP[34700:903] This is Phrase A, Hello!
2011-03-21 16:18:54.718 HelloOOP[34700:903] This is Phrase A, Hello!
2011-03-21 16:18:54.719 HelloOOP[34700:903] The string 'This is Phrase A, Hello!' has been printed 2 times
2011-03-21 16:18:54.719 HelloOOP[34700:903] Return from numOfPrints: 2

So as we discussed before, an integer is returned by our new method, so we can use that just like any other integer variable. For clarity, we could also store the return into a new variable before using it. Example:

  1. int varTest = [phraseA numOfPrints];
  2. NSLog(@"Return from numOfPrints: %i",varTest);

This will work the same as the code above.

If you remember way back in Part 3, I referred to the setText method as a “setter” method. Well, numOfPrints is considered a “getter” method. These are referred to as “Accessor” methods in programming parlance. Accesor methods are very simple methods that interact directly with the instance variables. Now, if you have many instance variables, this could be alot of coding if it were not for a rather neat feature of ObjC: Synthesizing.

Essentially, using synthesizing, we can quickly create methods for each variable we need. Lets hop right into the code and show you an example while adding another variable, timesToPrint. First, your PrintCount.H file should look like this:

  1. #import <Foundation/Foundation.h>
  2.  
  3.  
  4. @interface PrintCount : NSObject {
  5. int numOfPrints;
  6. int timesToPrint;
  7. NSString *textToPrint;
  8. }
  9. @property int numOfPrints, timesToPrint;
  10.  
  11. -(void) printText;
  12. -(void) printNum;
  13. -(void) setText: (NSString *) text;
  14.  
  15.  
  16. @end

And all you need to add to your PrintCount.m file is this line after your @implementation line:

  1. @synthesize numOfPrints, timesToPrint;

With those 2 lines of code, ObjC will automatically generated the following methods:
[instanceName numOfPrints] - Returns the value of numOfPrints
[instanceName setNumOfPrints] - Sets the numOfPrints variable
[instanceName timesToPrint] - Returns the value of numOfPrints
[instanceName setTimesToPrint] - Sets the timesToPrint variable

As you can see, its very easy to generate the methods using synthesize. Its important to note that this generates the methods using the common convention of the variable name being the getter, and the getter has set added to the beginning. If you build and run from this point, your code should still work since in the main program we are still calling numOfPrints. We will use the new variable timesToPrint when we start talking about logic in the next post.

Just as an aside, This was my first real hop into Xcode 4 and its pretty amazing. I was a bit skeptical about the one window IDE idea, but so far its a lot easier to navigate. Its very powerful though, and I know i need to learn more about how to utilize it better. Interestingly, they finally included GIT as part of the source control options, and totally dropped support for CVS. SCM is something we will discuss later. I found some amazing git tutorials, but its pretty advanced for people just starting out. Next up we will be using about decision making in our programs!



Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><img>
  • Lines and paragraphs break automatically.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options