Calvin's

Icon

designs and hacks. people and products.

iOS dev launcher: Session #1 SaveThePrincess

save the princess

Participated in Subhb’s iOS dev launcher workshop today and it was really fun to chill out and mess with code with a whole bunch of like-minded people.

Stuff for my Objective C Muscle Memory

Besides the social aspect of these coding workshops, it was also a great opportunity for me to get a refresher on Objective C since I last worked on it 6 months ago.  Here’s the gist of what session #1 covered, introduced in a cute and friendly way using a “Princess” class:-

  • Anatomy of an Objective C class
  • Creating our own custom class
  • Memory allocation when instantiating a class
  • Messaging Syntax (essentially methods in an Objective C class, whether it is a class method or an instance method)
  • Writing @property in a class in the header file (.h) and making them available in the implementation (.m) file with @synthesize
  • And @property of course is represented by this – @property (<attributes>) <type> <name>;
  • The differences between readonly, assign, copy and retain attributes in the @property declaration

Practice assignment for session #1

  • replicate “SaveThePrincess” Exercise
  • Add a “School” property to the princess class
  • Create a new initializer method to create a princess instance with name, age and school
  • Change sing method and print “Myra loves singing, she is 5 years old and she goes to Bugis Primary School”
  • Change description method to print school name for debugging

And so, giving the name, age and school to my princess object, my princess Kait-lyn is 6 years old and goes to school at Phyllis Riccia.

What about your princess? :-)

Source code right here – https://github.com/calvinchengx/SaveThePrincess

Memory Management: Autorelease Pools

An autorelease pool is an instance of NSAutoreleasePool and defines a scope for temporary objects.  These temporary objects are added into the current autorelease pool when we send them an autorelease message or we have created them using a convenience method.  When the autorelease pool is released, all the temporary objects added into it are automatically released.  This, of course, is much simpler than manually releasing individual objects which were created by alloc or retain!

Autorelease pools can be nested which means that autorelease objects are added to the latest autorelease pool to be created.