<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Defining a Class</title>
	<atom:link href="http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Sat, 13 Mar 2010 20:18:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Greg</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html/comment-page-1#comment-7331</link>
		<dc:creator>Greg</dc:creator>
		<pubDate>Mon, 30 Nov 2009 07:30:12 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=39#comment-7331</guid>
		<description>Thanks John, Yes -using @end. Above I was putting the conversion formula in the definition of (int) retrieveResult: (int)r; and asking it to return the result of the calculation. 

I changed this approach and now it works by having a -(void) convertToCelsius; method and then a  separate method that returns the value of the calculation. Here is the whole thing:

&lt;pre lang=&quot;objc&quot;&gt;
@interface TemperatureConvert : NSObject
{
  int fahrenheit;
  int celsius;
}
-(void) convertToCelsius;
-(void) setFahrenheit: (int) f;
-(float) celsius;
-(void) print;

@end

//--------@implementation section------

@implementation TemperatureConvert
-(void) print
{
  NSLog (@&quot;%i degrees fahrenheit converted to celsius is %i degrees celsius&quot;, fahrenheit, celsius);
}

-(void) setFahrenheit: (int) f
{
  fahrenheit = f;
}

-(int) fahrenheit
{
  return fahrenheit;
}

-(void) convertToCelsius
{
  celsius =(fahrenheit - 32) /1.8;
}

-(float) celsius
{
  return celsius;
}

@end

//--------@programsection--------

int main (int argc, const char * argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
  //create an instance of a TempConvert
  TemperatureConvert *myTemperatureConvert = [[TemperatureConvert alloc] init];

  //set Fahrenheit to 27 degrees
  [myTemperatureConvert setFahrenheit: 101];
	
  //calculate conversion to celsius 
  [myTemperatureConvert convertToCelsius];
		
  //display result of conversion
  NSLog(@&quot;%i degrees fahrenheit converted to celsius is %i degrees celsius&quot;,[myTemperatureConvert fahrenheit],[myTemperatureConvert celsius]);
	
  [myTemperatureConvert print];
  [myTemperatureConvert release];
	
    [pool drain];
    return 0;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Thanks John, Yes -using @end. Above I was putting the conversion formula in the definition of (int) retrieveResult: (int)r; and asking it to return the result of the calculation. </p>
<p>I changed this approach and now it works by having a -(void) convertToCelsius; method and then a  separate method that returns the value of the calculation. Here is the whole thing:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> TemperatureConvert <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span> fahrenheit;
  <span style="color: #a61390;">int</span> celsius;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> convertToCelsius;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setFahrenheit<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> f;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> celsius;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> print;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//--------@implementation section------</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> TemperatureConvert
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> print
<span style="color: #002200;">&#123;</span>
  NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%i degrees fahrenheit converted to celsius is %i degrees celsius&quot;</span>, fahrenheit, celsius<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setFahrenheit<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> f
<span style="color: #002200;">&#123;</span>
  fahrenheit <span style="color: #002200;">=</span> f;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> fahrenheit
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> fahrenheit;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> convertToCelsius
<span style="color: #002200;">&#123;</span>
  celsius <span style="color: #002200;">=</span><span style="color: #002200;">&#40;</span>fahrenheit <span style="color: #002200;">-</span> <span style="color: #2400d9;">32</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span><span style="color: #2400d9;">1.8</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> celsius
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> celsius;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//--------@programsection--------</span>
&nbsp;
<span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">//create an instance of a TempConvert</span>
  TemperatureConvert <span style="color: #002200;">*</span>myTemperatureConvert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TemperatureConvert alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">//set Fahrenheit to 27 degrees</span>
  <span style="color: #002200;">&#91;</span>myTemperatureConvert setFahrenheit<span style="color: #002200;">:</span> <span style="color: #2400d9;">101</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">//calculate conversion to celsius </span>
  <span style="color: #002200;">&#91;</span>myTemperatureConvert convertToCelsius<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">//display result of conversion</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%i degrees fahrenheit converted to celsius is %i degrees celsius&quot;</span>,<span style="color: #002200;">&#91;</span>myTemperatureConvert fahrenheit<span style="color: #002200;">&#93;</span>,<span style="color: #002200;">&#91;</span>myTemperatureConvert celsius<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>myTemperatureConvert print<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>myTemperatureConvert release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: John Muchow</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html/comment-page-1#comment-7202</link>
		<dc:creator>John Muchow</dc:creator>
		<pubDate>Fri, 27 Nov 2009 19:57:53 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=39#comment-7202</guid>
		<description>Hey Greg,

I assume you are closing the interface with an &quot;@end&quot; statement? I don&#039;t get an error when compiling the above, you may have an error in the implementation file...

John</description>
		<content:encoded><![CDATA[<p>Hey Greg,</p>
<p>I assume you are closing the interface with an &#8220;@end&#8221; statement? I don&#8217;t get an error when compiling the above, you may have an error in the implementation file&#8230;</p>
<p>John</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html/comment-page-1#comment-7200</link>
		<dc:creator>Greg</dc:creator>
		<pubDate>Fri, 27 Nov 2009 19:39:19 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=39#comment-7200</guid>
		<description>I am new to programming and I am learning Obj C. I am stuck on a tutorial where I keep getting an error saying that my getter &quot;r&quot; is undeclared even though I have declared it in the @interface section.

Perhaps someone is willing to take a look and tell me what I am doing wrong? Here is the @ interface section:

@interface TemperatureConvert : NSObject
{
	int fahrenheit;
	int celsius;
	int result;
	
}
-(int) retrieveResult: (int) r;
-(void) setFahrenheit: (int) f;
-(void) setCelsius: (int) c;
-(void) print;


The voids all work fine... just the getter -(int) retrieveResult: (int) r; produces an error in the program section saying that r is not declared.

Any thoughts?

Greg</description>
		<content:encoded><![CDATA[<p>I am new to programming and I am learning Obj C. I am stuck on a tutorial where I keep getting an error saying that my getter &#8220;r&#8221; is undeclared even though I have declared it in the @interface section.</p>
<p>Perhaps someone is willing to take a look and tell me what I am doing wrong? Here is the @ interface section:</p>
<p>@interface TemperatureConvert : NSObject<br />
{<br />
	int fahrenheit;<br />
	int celsius;<br />
	int result;</p>
<p>}<br />
-(int) retrieveResult: (int) r;<br />
-(void) setFahrenheit: (int) f;<br />
-(void) setCelsius: (int) c;<br />
-(void) print;</p>
<p>The voids all work fine&#8230; just the getter -(int) retrieveResult: (int) r; produces an error in the program section saying that r is not declared.</p>
<p>Any thoughts?</p>
<p>Greg</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Akit Thakur</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html/comment-page-1#comment-17</link>
		<dc:creator>Akit Thakur</dc:creator>
		<pubDate>Sun, 17 Aug 2008 01:07:12 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=39#comment-17</guid>
		<description>Thanks for posting such an awesome article on objective C for intiators. I am from Java background, and currently shifted to Cocoa Objective C- Java Development in iPhone. 

With this article I have realized that Programming in Objective-C is very much similar to Java except syntax if considering import of files, getter and setter methods and their return types.

I am trying to create a directory where I want to save the Mobile no and Name of the person and want to same it in the iPhone memry. For this I am using XCode IDE. 

Can you please guide me, how can I save data in iPhone memory.

Waiting for your new article on saving data in iPhonr memory.

With regards,

Ankit Thakur</description>
		<content:encoded><![CDATA[<p>Thanks for posting such an awesome article on objective C for intiators. I am from Java background, and currently shifted to Cocoa Objective C- Java Development in iPhone. </p>
<p>With this article I have realized that Programming in Objective-C is very much similar to Java except syntax if considering import of files, getter and setter methods and their return types.</p>
<p>I am trying to create a directory where I want to save the Mobile no and Name of the person and want to same it in the iPhone memry. For this I am using XCode IDE. </p>
<p>Can you please guide me, how can I save data in iPhone memory.</p>
<p>Waiting for your new article on saving data in iPhonr memory.</p>
<p>With regards,</p>
<p>Ankit Thakur</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad W. Taylor</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html/comment-page-1#comment-16</link>
		<dc:creator>Chad W. Taylor</dc:creator>
		<pubDate>Tue, 12 Aug 2008 01:05:53 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=39#comment-16</guid>
		<description>Thanks for posting, especially the comment by Brian.  I am coming to Obj-C from .NET (mainly C#) and boy, I&#039;m having the hardest time trying to get in tune with the new syntaxes.

I always use properties (getters and setters) in C# so this definitely helps me understand how to set them up in Obj-C.  Same goes for the foreach loop in C# when looping through enumerated objects and seeing how it&#039;s done in Obj-C.

I&#039;m glad to see that Obj-C 2.0 has introduced simpler ways to set properties and iteration loops or I&#039;d have given up.  Well, probably not since I love the machine! 

Keep &#039;em coming!</description>
		<content:encoded><![CDATA[<p>Thanks for posting, especially the comment by Brian.  I am coming to Obj-C from .NET (mainly C#) and boy, I&#8217;m having the hardest time trying to get in tune with the new syntaxes.</p>
<p>I always use properties (getters and setters) in C# so this definitely helps me understand how to set them up in Obj-C.  Same goes for the foreach loop in C# when looping through enumerated objects and seeing how it&#8217;s done in Obj-C.</p>
<p>I&#8217;m glad to see that Obj-C 2.0 has introduced simpler ways to set properties and iteration loops or I&#8217;d have given up.  Well, probably not since I love the machine! </p>
<p>Keep &#8216;em coming!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stevebert</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/defining-a-class.html/comment-page-1#comment-15</link>
		<dc:creator>stevebert</dc:creator>
		<pubDate>Mon, 11 Aug 2008 01:04:31 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=39#comment-15</guid>
		<description>Great article!  I would also recommend that folks new to Objective-C (folks familiar with it, too!) take a look at 2 new features of Objective-C 2.0 introduced in 10.5/Leopard: properties and fast enumeration.

Using ObjC2 properties forgoes the need to write getter-setter methods entirely:

@interface SomeClass : NSObject
{
    NSString *_str;
    NSDate *_date;
    int _x
}
@property(retain, readwrite) NSString *str;
@property(retain, readwrite) NSDate *date;
@property(assign, readwrite) int x;
:

@implementation SomeClass
@synthesize str = _str;
@synthesize date = _date;
@synthesize x = _x;

In this case the @property declaration, combined with the @synthesize, will automatically generate getter/setter methods for each instance variable -- a real time-saver when your class declares a 20 variables (or even if you only declare 1).  Since most getter/setter implementations follow the same code template, this can be a nice time saver and leaves the code less cluttered and easier to maintain.

Properties declared with (retain, ...) will automatically call [NSObject retain] when a new value is assigned to the instance variable via the synthesized setter, as well as [NSObject release] for the previously assigned object.  The &quot;assign&quot; modifier tells the compiler to generate a simple assignment, useful for non-Cocoa variable types, or NSObject references which do not need to be retained.  Variables can alternately be declared &quot;readonly&quot;, which generates compiler errors if the code attempts to make an assignment.  These are the most common modifiers -- the Objective-C 2.0 guide defines others for special cases.

Concurrent with the new property declarations is the change to &quot;.&quot; notation for accessing getter/setting methods.

SomeClass *anInst = [[[SomeClass alloc] init] autorelease];
anInst.str = @&quot;Hello, world!&quot;; // retained
anInst.date = [NSDate date]; // retained
anInst.x = 5;  // assigned

The underscore (&quot;_&quot;) notation in the instance variable declarations are optional, but a useful way to distinguish direct variable references from getter/setter references in your code.

@implementation SomeClass
:
- (void) setDefaultX
{
    _x = 5; // direct assignment
}

Once you get used to the property syntax, the older form of Objective-C feels clunky.

Fast enumeration is a newer way to iterate through NSArray collections.  The traditional way of writing iteration loops is familiar to most Cocoa programmers:

NSArray *aList = [myClass getListOfObjects];
NSEnumerator *anEnum = [aList objectEnumerator];
NSString *aStr = nil;
while (nil != (aStr = [anEnum nextObject])) {...}

With fast enumeration, the same code appears as follows:

NSArray *aList = [myClass getListOfObjects];
for (NSString *aStr in aList) {...}

Not only is it less code, which is always a good thing, but the fast enumeration form allows the compiler to make optimizations in the loop interation that can be several times faster than the older NSEnumerator-based loops.

I find these new features of Objective-C 2.0 to be very handy in writing more compact, readable code, which lets me concentrate more on the logic of my own application, rather than the syntactic overhead of writing classes.

Enjoy!</description>
		<content:encoded><![CDATA[<p>Great article!  I would also recommend that folks new to Objective-C (folks familiar with it, too!) take a look at 2 new features of Objective-C 2.0 introduced in 10.5/Leopard: properties and fast enumeration.</p>
<p>Using ObjC2 properties forgoes the need to write getter-setter methods entirely:</p>
<p>@interface SomeClass : NSObject<br />
{<br />
    NSString *_str;<br />
    NSDate *_date;<br />
    int _x<br />
}<br />
@property(retain, readwrite) NSString *str;<br />
@property(retain, readwrite) NSDate *date;<br />
@property(assign, readwrite) int x;<br />
:</p>
<p>@implementation SomeClass<br />
@synthesize str = _str;<br />
@synthesize date = _date;<br />
@synthesize x = _x;</p>
<p>In this case the @property declaration, combined with the @synthesize, will automatically generate getter/setter methods for each instance variable &#8212; a real time-saver when your class declares a 20 variables (or even if you only declare 1).  Since most getter/setter implementations follow the same code template, this can be a nice time saver and leaves the code less cluttered and easier to maintain.</p>
<p>Properties declared with (retain, &#8230;) will automatically call [NSObject retain] when a new value is assigned to the instance variable via the synthesized setter, as well as [NSObject release] for the previously assigned object.  The &#8220;assign&#8221; modifier tells the compiler to generate a simple assignment, useful for non-Cocoa variable types, or NSObject references which do not need to be retained.  Variables can alternately be declared &#8220;readonly&#8221;, which generates compiler errors if the code attempts to make an assignment.  These are the most common modifiers &#8212; the Objective-C 2.0 guide defines others for special cases.</p>
<p>Concurrent with the new property declarations is the change to &#8220;.&#8221; notation for accessing getter/setting methods.</p>
<p>SomeClass *anInst = [[[SomeClass alloc] init] autorelease];<br />
anInst.str = @&#8221;Hello, world!&#8221;; // retained<br />
anInst.date = [NSDate date]; // retained<br />
anInst.x = 5;  // assigned</p>
<p>The underscore (&#8220;_&#8221;) notation in the instance variable declarations are optional, but a useful way to distinguish direct variable references from getter/setter references in your code.</p>
<p>@implementation SomeClass<br />
:<br />
- (void) setDefaultX<br />
{<br />
    _x = 5; // direct assignment<br />
}</p>
<p>Once you get used to the property syntax, the older form of Objective-C feels clunky.</p>
<p>Fast enumeration is a newer way to iterate through NSArray collections.  The traditional way of writing iteration loops is familiar to most Cocoa programmers:</p>
<p>NSArray *aList = [myClass getListOfObjects];<br />
NSEnumerator *anEnum = [aList objectEnumerator];<br />
NSString *aStr = nil;<br />
while (nil != (aStr = [anEnum nextObject])) {&#8230;}</p>
<p>With fast enumeration, the same code appears as follows:</p>
<p>NSArray *aList = [myClass getListOfObjects];<br />
for (NSString *aStr in aList) {&#8230;}</p>
<p>Not only is it less code, which is always a good thing, but the fast enumeration form allows the compiler to make optimizations in the loop interation that can be several times faster than the older NSEnumerator-based loops.</p>
<p>I find these new features of Objective-C 2.0 to be very handy in writing more compact, readable code, which lets me concentrate more on the logic of my own application, rather than the syntactic overhead of writing classes.</p>
<p>Enjoy!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
