Objective-C Scoping, Nil Object and Static Variable

Objective-C Memory Management (Simple overview)

Objective-C uses reference count to track whether an Objective-C object can be free. The runtime performs the actual memory freeing

  • Use retain to increase a reference count
    [value retain]
  • Use release to decrease the reference count
    [value release]
    • Once the count is drop to zero, the runtime can garbage collect the object
  • autorelease also decreases the reference count
    • But even the count is drop to zero, the object will not ready for garbage collection until the whole method call chain is completed
      [value autorelease]

Memory Management in the setter

Use setter method to manage the reference count

- (void) setValue: (NSString*)input
{
    [value autorelease];
    value = [input retain];
}
  • Decrease the reference count of the old value
  • Increase the reference count of the new input value
  • Use autorelease instead of the release
    • if we set the variable with the same old object, the first release may release the object input before the reference count is increased again

Static and Global Variables for Objective-C

Create a variable shared among instances of the same class

static MyString* MySingletonInstance;

@implementation MyString

+ (MyString *) instance
    if ( MySingletonInstance == nil ) {
        MySingletonInstance = [[self alloc] init];
    }
    return MySingletonInstance;
}

Retrieve the static variable

MyString *v = [MyString instance];

Implement Class constant

int MyConstant;

@implementation MyString
...

Initialize static or global variables

+ (void)initialize
{
  // Avoid multiple initialize called by its children
  if (self == [MyString class]) {
        ...
    }
}

Nil Objective-C Object

obj1  = nil;
result = [obj1 somemethod];
  • Call method on a nil object will return a nil object
  • Provide a mechanism of not verifying nil object every time

Alternative way in implement dealloc

- (void) dealloc
{
    self.value = nil;
    [super dealloc];
}
self.value = nil;
  • Call the setter method which will automatically release the old object and set the instance variable to nil
  • A preferable way to implement dealloc

Instance Variable Scoping

Control the scoping of Objective-C instance variable

@interface MyString : NSObject
{
    char *name;
@private
    // Accessible by this class only
    char* privateData;
@protected
    // Also accessible by children class
    float protectedData;
@public
     // Accessible by everyone
    id publicData;
}
  • Default is @protected

Object Association

Associate 2 Objective-C objects

static char key;

NSString *v1 = [[NSString alloc] init];
NSString *v2 = [[NSString alloc] init];

objc_setAssociatedObject (
    v1,
    &key,
    v2,
    OBJC_ASSOCIATION_RETAIN
);

Retrieve associated objects

NSString *associate = (NSString *)objc_getAssociatedObject(v1, &key);

Break the association

objc_setAssociatedObject(v1, &key, nil, OBJC_ASSOCIATION_ASSIGN);