<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Giordano Scalzo&#039;s Personal Blog &#187; objective-c</title>
	<atom:link href="http://giordano.scalzo.biz/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Fri, 09 Jul 2010 09:41:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Objective-C for busy Java Developers 1: Calling methods</title>
		<link>http://giordano.scalzo.biz/2010/01/19/objective-c-for-busy-java-developers-1-calling-methods/</link>
		<comments>http://giordano.scalzo.biz/2010/01/19/objective-c-for-busy-java-developers-1-calling-methods/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 13:34:43 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=261</guid>
		<description><![CDATA[At last I got a wonderful MacBookPro, so I started to study Objective-C to develop some cool Iphone applications.
Objective-C is a language derived from C, to which it adds some modern features as ObjectOriented or Smalltalk-style messaging.
As far I&#8217;m a complete newbie, I&#8217;m trying to learn it recalling some well know patterns and scenarios as [...]]]></description>
			<content:encoded><![CDATA[<p>At last I got a wonderful MacBookPro, so I started to study <a href="http://en.wikipedia.org/wiki/Objective-C">Objective-C</a> to develop some cool Iphone applications.</p>
<p><a href="http://en.wikipedia.org/wiki/Objective-C">Objective-C</a> is a language derived from C, to which it adds some modern features as ObjectOriented or Smalltalk-style messaging.</p>
<p>As far I&#8217;m a complete newbie, I&#8217;m trying to learn it recalling some well know patterns and scenarios as made in Java , following <a href="http://cocoadevcentral.com/d/learn_objectivec/">this good tutorial</a>.</p>
<p><a href="http://en.wikipedia.org/wiki/Objective-C">Objective-C</a> has a little strange way to call method, that could be disorienting at first glance:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
object.method;
object.methodWithInput(input);

output = object.methodWithOutput();
output = object.methodWithInputAndOutput(Object input);
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
[object method];
[object methodWithInput:input];

output = [object methodWithOutput];
output = [object methodWithInputAndOutput:input];
</pre>
<p>Obviously, it&#8217;s possible to call methods of class, instead of instance:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
Object oString = new String();
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
id oString = [NSString string];
</pre>
<p>The
<pre>id</pre>
<p> refers any kind of object, so it&#8217;s little different from Java counterpart.<br />
Better code is:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
String sString = new String();
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
NSString* sString = [NSString string];
</pre>
<p>With this style, it&#8217;s a little cumbersome write nested calls:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
calculator.add(numbers.split());
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
[calculator add:[numbers split]];
</pre>
<p>This syntax disencourage the nesting of more than one method.</p>
<p>Some methods take multiple input arguments, Objective-C deals with that allowing split method names:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
boolean writeToFile(String path, boolean useAuxiliaryFile)

boolean result = myData.writeToFile("/tmp/log.txt", false);
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
-(BOOL)writeToFile:(NSString *)path withAuxFile:(BOOL)useAuxiliaryFile;

BOOL result = [myData writeToFile:@"/tmp/log.txt" withAuxFile:NO];
</pre>
<p>Objective-C has properties built in, in Java you need to implement getters and setters:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
photo.setCaption("Day at the Beach");
output = photo.getCaption();
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
photo.caption = @"Day at the Beach";
output = photo.caption;
</pre>
<p>A property should be marked
<pre>@property</pre>
<p> in declaration and
<pre>@synthesize</pre>
<p> in implementation.</p>
<p>To create an object, the function
<pre>alloc</pre>
<p> should be called and then an init method should be called:</p>
<p><b>Java</b>:</p>
<pre class='brush:java'>
object = new ComplexObject(1.0f);
</pre>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
object = [[ComplexObject alloc] initWithFloat:1.0f];
</pre>
<p>When working in an environment without garbage collector, any object created with alloc should be released:</p>
<p><b>Objective-C</b>:</p>
<pre class='brush:c'>
[object release];
</pre>
<p>To complete this introductory post, take a look at this <a href='http://giordano.scalzo.biz/wp-content/uploads/2010/01/objectiveccheatsheet.pdf'>ObjectiveC CheatSheet</a>: it contains all the most used constructs needed to start to code for Mac.</p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/java' rel='tag' target='_self'>java</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/tutorial' rel='tag' target='_self'>tutorial</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2010/01/19/objective-c-for-busy-java-developers-1-calling-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
