Get $500+ of the best After Effects files, video templates and music for only $20!
Q&A Session 3: iOS Memory Management and Best Practices

    Q&A Session 3: iOS Memory Management and Best Practices

    In this week’s Q&A session, Mark Hammonds answers questions left on Axit Patel’s post “Working with the UITableView Class, Part 2.” Specifically, this Q&A discusses memory management techniques and the ability to “autorelease” objects. Mark also talks about best practices when using viewDidLoad and other tips related to NSString.

    Questions Answered:

    Tutorial: iPhone SDK: Working with the UITableView Class – Part 2

    Question:
    1) In order to fill the imagesArray you create a couple of UIImageViews, but you don’t release them later. Is this okay or did you just forget to do it?
    2) Why do you create the dummy arrays anyway? Couldn’t you just say self.sitesArray = [[NSArray alloc] initWithObjects:@”..”, … , nil]; ?
    Or is there a reason to do it the way you did?

    From: Dan

    Errata:

    • At 6:40 in the video I state that using @”Hello World!” will return an auto-released string. This is incorrect. It is instead a statically allocated string that is kept around throughout the program lifecycle, as pointed out in the comments section below.
    • At 7:31 I state that %@ is the format specifier for an NSString. Technically, this is correct, but it is important to note that %@ is also used to print information on other Cocoa-Touch objects as well. An object formatted with %@ will use the -description instance method originally inherited from NSObject to display custom output for that object. NSString implements this to display the string value, other objects behave in different ways.

    Add Comment

    Discussion 4 Comments

    1. Cesar says:

      Hello Mark, thanks for your posts and the site which is really great.

      I’d like to make some comments on two little things I noticed during the screencast:

      1.It was unclear, but just to remember, the %@ you used in +stringWithFormat represents strings, yes, but also any object. That’s because NSObject has a -description method which is automatically called, it returns a NSString * and any class can overwrite to provide more information.

      2.In this code:
      NSString *string = @”Hello World!”
      ‘string’ is not an autoreleased object! Statically allocated strings are kind of “special”. They do not follow usual retain count, and are kept around during the program lifecycle.

      Try this:

      NSString *string = @”Hello World”;

      [string retain];
      [string retain];
      [string retain];

      NSLog(@”%d”, [string retainCount]);

      As well as everything beginning with ‘#’ is a preprocessor directive, ‘@’ indicates a compiler directive.

      • Author

        Hi Cesar,

        You’re absolutely right on both points!

        With regard to #1, I should have been more clear in indicating that “%@” is not just used for strings, but can also be used by other objects with -description as well. With regard to #2, I recall reading this at some point in the past (Stephen Kochan’s Objective-C 2.0 book, perhaps), but I had completely forgotten about that fact while shooting this screencast. The hard part about doing these sessions is so much of it comes “off the cuff” and I can’t just edit one or two sentences after the fact. However, I will update this post with an “errata” section to clearly point out the two issues you’ve raised here. Mea Culpa!

        Thanks again for your participation! Comments like this definitely help make Mobiletuts+ the best site it can possibly be.

        Cheers,
        Mark Hammonds

        • Cesar says:

          Thanks Mark!

          Indeed programming involves many little concepts, and it’s quite easy to skip some of them while you’re focusing on explaining another thing. It’s completely normal, and that’s one reason for the comments section existence :-)

          Best Wishes

    2. Dan says:

      Thank you, Mark.
      This was really helpful! Enjoyed watching!
      Keep up the great work!

    Add a Comment

    To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.