    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: An Introduction to Objective-C</title>
        <link>https://members.accu.org/index.php/articles/691</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 16, #5 - Oct 2004</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c100/">165</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-100/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+100/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;An Introduction to Objective-C</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 October 2004 13:16:07 +01:00 or Sun, 03 October 2004 13:16:07 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a></h2>
</div>
<p>Unlike C++, Objective-C is standard C with a small
object-oriented extension; an Objective-C compiler will compile all
conforming C code in exactly the same way as a C compiler.</p>
<p>Objective-C source files traditionally have the suffix
<tt class="filename">.m</tt>; and with the Apple/NeXT compiler, a
file that contains a mixture of Objective-C and C++ code (so-called
Objective C++) needs to be suffixed with <tt class=
"filename">.mm</tt>.</p>
<p>Objective-C adds one type to the C language, namely <tt class=
"type">id</tt>, which is a pointer to any object.</p>
<pre class="programlisting">
id myObject;
</pre>
<p>declares an object pointer with the name <tt class=
"type">myObject</tt>.</p>
<p>If the class of the object is known, the above declaration is
equivalent to:</p>
<pre class="programlisting">
MyClass *myObject;
</pre>
<p>which declares a pointer to an object of class <tt class=
"classname">MyClass</tt>. This syntax allows the compiler to
perform static type-checking, so that the programmer can be
informed at compile time that an object of that class does not
support a particular operation.</p>
<p>In Objective-C, unlike C++, all objects are referenced by means
of pointers.</p>
<p>Classes are declared as follows:</p>
<pre class="programlisting">
#import &quot;BaseClass.h&quot;

@interface MyClass : BaseClass
{
  // Instance variables
  int n;
  float x;
}

// Method declarations

// Class method
+ (id)new;

// Instance method with arguments
- (id)initWithValues:(int)nn and:(float)xx;

@end
</pre>
<p>This is the public interface of the class <tt class=
"classname">MyClass</tt>, which inherits from <tt class=
"classname">BaseClass</tt>.</p>
<p>By default, instance variables have 'protected' visibility; this
means that they can be read and written to directly only by objects
of the class in which they are declared or of a class inheriting
from it. The default visibility can be varied by means of the
compiler directives <tt class="literal">@public</tt> and <tt class=
"literal">@private</tt>; public variables are visible to all
outside code, while private ones are accessible only to instances
of the class in which they are declared.</p>
<p>A class method is prefixed by + and could be called in the
following manner to create an instance of the class and its address
stored in a variable:</p>
<pre class="programlisting">
MyClass *newObject = [MyClass new];
</pre>
<p>Every class is an object in Objective-C and typically contains
methods, like new above, to create instances of itself.</p>
<p>The return type of a method and the type of its arguments are
written within parentheses. If the type is not provided, type id is
assumed. It is important to understand that type names within
parentheses resemble the C syntax for 'casting' a value from one
type to another, in this context no casting is involved.</p>
<p>Instance methods belong to an instance of the class and can
access its instance variables. Their declarations are preceded by
the token -.</p>
<p>Arguments are declared after keywords terminating in colons; the
name of the method is understood to consist of all the keywords
together with the colons. The instance method above has the name
<tt class="methodname">initWithValues:and:</tt>, and it is
pronounced: 'init with values colon and colon'. The method could be
invoked in the following manner:</p>
<pre class="programlisting">
[newObject initWithValues:42 and:12.576];
</pre>
<p>This style of naming methods will initially seem strange to
those who are unfamiliar with Smalltalk, from which it is
derived.</p>
<p>The implementation of a class is defined as follows:</p>
<pre class="programlisting">
#import &quot;MyClass.h&quot;

@implementation Myclass

+ (id)new
{
  // Allocate memory for an object and
  // initialise to default values.
  return [[super alloc] init];
}

// -init is a private method and is thus not
// declared in the interface.
- (id)init
{
  // Initialise self to default values.
  return [self initWithValues:0 and:0.0];
}

- (id)initWithValues:(int)nn and:(float)xx
{
  // Call BaseClass init method.
  [super init];

  // Initialise the instance variables and
  // return a pointer to this object.
  n = nn;
  x = xx;
  return self;
}

@end
</pre>
<p>The keyword <tt class="literal">self</tt> is a pointer to the
current object; super is a directive that instructs the compiler to
invoke a method in the superclass (i.e. the class from which the
current one is derived, otherwise known as 'base class').</p>
<p>The normal way to invoke methods in Objective-C is the write the
object name (known as the receiver) followed by the method name
(with arguments, if required) between square brackets; the whole is
known as a message expression.</p>
<p>The method name is called a 'selector' because the receiving
object uses it to select the appropriate method for the required
operation. The message expression:</p>
<pre class="programlisting">
[self initWithValues:0 and:0.0]
</pre>
<p>above could be described in English as: 'send self a message
with selector <tt class="methodname">initWithValues</tt> and
parameters 0 and 0.0'.</p>
<p>The reader should now have enough information to know how to
write Objective-C classes, how instances are created and how to
invoke methods.</p>
<p>Apart from knowledge of supplied class libraries, little else is
required in order to become a competent user of the language.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
