Objective-C: Retain, Release, Autorelease

The rules for -retain, -release, and -autorelease are simple. If you alloc, copy, or retain something, you also need to (eventually) release or autorelease it. If you don't do any of those three things, you don't need to do any memory management at all. Not only don't need to, if you try, it messes things up.

If you alloc something, it's just like you sent -retain to the object. So you release or autorelease it. If you do a copy or a mutableCopy, it's also the same as sending a retain. So you also release or autorelease it.

If you never sent a retain (or equivalent) method to an object, then by default it is autoreleased and will go away on its own. The following example doesn't need to be released explicitly since it has already been given an -autorelease message internally. (see NSArray Class Reference -> arrayWithObjects:)
NSArray * sampleList = [NSArray arrayWithObjects:@"One", @"Two", nil];

Also, in my code I always try to release an object instead of autoreleasing it as much as possible.

The actual mechanism here is that when you call -autorelease, a pointer to the object is added to a list kept by an NSAutoreleasePool. When that pool is deallocated, every object in the list gets a -release method. Thus -autorelease is a delayed -release.

Subclassing UIView with NIB file

Here's how UIView subclassing is done (with NIB files). I felt the need of doing so for two reasons: reusability and maintainability.

Header class:
#import <UIKit/UIKit.h>

@interface MyCustomView: UIView

+ (MyCustomView *)createWithOwner:(id)owner;

@end

Implementation class:
#import "MyCustomView.h"

@implementation MyCustomView

+ (MyCustomView *)createWithOwner:(id)owner {
  NSString *nibName = NSStringFromClass([self class]);
  NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];
  MyCustomView *view = [nib objectAtIndex:0];
  return view;
}

@end

Using the the new class is as simple as this:
#import "MyCustomView.h"

...

MyCustomView * customView = [MyCustomView createWithOwner:self];
[self.view addSubView: customView];
[customView release];  

...

Related posts:

Bookmark and Share

Determine if UITableView is near bottom when scrolling

Here's my way of finding out if UITableView is near bottom when scrolling. And when it does, we fetch and load another batch of items.

Facebook `comments.remove` API Method

With FB's Comment Social plugin, having to remove comments on a certain Application is a click away as long as the current user has the Admin rights. This thing right here may be necessary to those who wanted a custom Administration sort of utility for managing FB App related comments.

Here's a sample code of how one could implement this method:

Facebook fb:multi-friend-selector In Page Tab

To those spending days and hours searching for a solution on putting an fb:multi-friend-selector inside a Page Tab, well, sadly you won't find anything out there. The challenge is to make it work inside a page tab. Note that page tab is restricted to 520 pixels wide only. So no matter what you do, you'd still get this dialog box cut off since it's stuck at 580px wide.

In our case, we tried a different approach. We went using window popups. This way we could still display the multi-friend-selector preview dialog in its entirety.

I think fb:multi-friend-selector is only intended in Canvas Page. But that doesn't hinder us now.

Grab the Picture of a Facebook Graph Object

Here's what you need to do to get to the actual location of a Facebook profile picture using PHP cURL (this comes in handy if you plan to store the picture somewhere else). In particular, we can grab the pictures of People, Events, Groups, Pages, Applications, and Photo Albums (these are called objects in Graph API).

A note on GIT's .gitignore

Here's an excerpt from Nick Quaranto's "Ignoring Files" article. I tend to forget this all the time.

Convert text URLs or Hyperlinks to clickable links

Here's a PHP code that can parse text URLs or Hyperlinks into a clickable link. This is useful when displaying text contents with inline links written along.

Tips on Developing Silverstripe Applications

Here's a handful of tips dedicated to all Silverstripe CMS developers out there (because I myself develop SilverStripe applications too).

Tips on Upgrading to Silverstripe CMS version 2.4

I just switched to Silverstripe 2.4+ and came accross several changes in contrast with SS2.3+. I'm gonna list here few of those changes so you guys will be aware of them. And I pledge to update this from time to time. Hopefully, this will guide you when you finally decide to upgrade your old version. Or this might as well motivate you to upgrade like me.