Objective c

659 views

Published on

  • Be the first to comment

  • Be the first to like this

Objective c

  1. 1. Objective-C - an introduction Spencer Pieters RovingBird
  2. 2. RovingBird
  3. 3. RovingBird
  4. 4. RovingBird
  5. 5. RovingBird
  6. 6. RovingBird
  7. 7. Experience C# C Other C++ Objective-C 14% 4% 36% 43% 4%
  8. 8. Topics History Classes Objects Memory Management OO Interesting Bits Recommended Books
  9. 9. 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.”
  10. 10. popularity++;
  11. 11. 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
  12. 12. What ? “Objective-C is a reflective, object-oriented programming language which adds Smalltalk-style messaging to the C programming language.”
  13. 13. Class Definition @interface Classname: ItsSuperclass { instance variable definitions } + (void)someMethod; - (void)anotherMethod; @end
  14. 14. Class Implementation @implementation Classname method definitions @end
  15. 15. Objects All objects are of type id Every object has isa instance variable Used by runtime to find out stuff about objects
  16. 16. Make objects do stuff [receiver message]; Runtime resolves target of the message Receiving object interprets message
  17. 17. Message examples [myRect display]; [myRect setWidth:20]; [myRect setOriginX:10 y:20];
  18. 18. Messaging how-to [object <keyword>:(type)<argument>]
  19. 19. Message examples if (x.insersectsArc(35, 19, 23, 90, 120))
  20. 20. Message examples if (x.insersectsArc(35, 19, 23, 90, 120)) if ([x intersectsArcWithRadius:35 centeredAtX:19 Y:23 fromAngle:90 toAngle:120])
  21. 21. Memory (1/3) retainCount new/alloc/copy: retainCount = 1 [object retain]: retainCount increase [object release]: retainCount decrease
  22. 22. Memory (2/3)
  23. 23. Memory (2/3) When retainCount becomes zero, object is free
  24. 24. Memory (3/3) NSArray *cars = [[NSArray alloc] init...]; Car *car = [[Car alloc] init]; [cars addObject:car]; [car release]; car = nil;
  25. 25. Categories Add new methods to existing classes without subclassing @interface NSString (CategoryName) - (NSString*)firstChar; @end
  26. 26. Limitations You can’t add instance variables to classes Collisions in method names (prefixes)
  27. 27. Fast Enumeration for (Type newVariable in expression) { statements } Expression conforms to NSFastEnumeration
  28. 28. Protocols List of methods Make a class conform to a protocol Aka interfaces @interface ClassName <ProtocolName> @required @optional @end
  29. 29. 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.
  30. 30. dot syntax car.color = whatever; [car setColor:whatever]; [car setValue:whatever forKey:@”color”];
  31. 31. Recommended Books
  32. 32. More recommended http://developer.apple.com/library/mac/#documentation/ Cocoa/Conceptual/ObjectiveC/ObjC.pdf
  33. 33. Interested in Cocoa? http://www.cocoaheads.be
  34. 34. More Info www.rovingbird.com spencer@rovingbird.com

×