Objective-C - an introduction
          Spencer Pieters
            RovingBird
RovingBird
RovingBird
RovingBird
RovingBird
RovingBird
Experience
  C#                 C
  Other              C++
  Objective-C



       14%
  4%            36%




   43%          4%
Topics
     History
      Classes
     Objects
Memory Management
       OO
  Interesting Bits
Recommended Books
popularity++;
          June 2010, TIOBE index

 “Despite the fact that Objective-C only gained
0.08% last month, it has entered the top-10 for
                the first time.”
popularity++;
History
   Created by Brad Cox and Tom Love
         (StepStone, early 1980)

            Licensed by NeXT
Used to build NeXTstep and project builder
            Used in Mac OS X
   Now: Xcode, Interface Builder, Cocoa
What ?
“Objective-C is a reflective, object-oriented programming
language which adds Smalltalk-style messaging to the C
                programming language.”
Class Definition
@interface Classname: ItsSuperclass
{
  instance variable definitions
}

+ (void)someMethod;
- (void)anotherMethod;

@end
Class Implementation

   @implementation Classname

   method definitions

   @end
Objects
           All objects are of type id


    Every object has isa instance variable
Used by runtime to find out stuff about objects
Make objects do stuff
         [receiver message];


Runtime resolves target of the message
 Receiving object interprets message
Message examples
      [myRect display];
    [myRect setWidth:20];
 [myRect setOriginX:10 y:20];
Messaging how-to

[object <keyword>:(type)<argument>]
Message examples
if (x.insersectsArc(35, 19, 23, 90, 120))
Message examples
if (x.insersectsArc(35, 19, 23, 90, 120))


  if ([x intersectsArcWithRadius:35
             centeredAtX:19
                  Y:23
              fromAngle:90
              toAngle:120])
Memory (1/3)
            retainCount


   new/alloc/copy: retainCount = 1
[object retain]: retainCount increase
[object release]: retainCount decrease
Memory (2/3)
Memory (2/3)




When retainCount becomes zero, object is free
Memory (3/3)

NSArray *cars = [[NSArray alloc] init...];
      Car *car = [[Car alloc] init];
          [cars addObject:car];
              [car release];
                car = nil;
Categories
Add new methods to existing classes without subclassing


        @interface NSString (CategoryName)

        - (NSString*)firstChar;

        @end
Limitations
You can’t add instance variables to classes
 Collisions in method names (prefixes)
Fast Enumeration
   for (Type newVariable in expression)
   {
      statements
   }

Expression conforms to NSFastEnumeration
Protocols
            List of methods
  Make a class conform to a protocol
             Aka interfaces

@interface ClassName <ProtocolName>

@required
@optional

@end
Interesting bits
In Objective-C it is valid to send a message to nil
                Boolean:YES/NO
            No abstract/final classes
 Exception handling via @try, @catch, @finally
       No namespaces. Prefixes are used.
dot syntax
         car.color = whatever;
        [car setColor:whatever];
[car setValue:whatever forKey:@”color”];
Recommended Books
More recommended

http://developer.apple.com/library/mac/#documentation/
        Cocoa/Conceptual/ObjectiveC/ObjC.pdf
Interested in Cocoa?


   http://www.cocoaheads.be
More Info


  www.rovingbird.com
spencer@rovingbird.com

Objective c