Pablo
Pablo

Reputation: 29519

What is modern runtime?

Note: Typically in a dealloc method you should release object instance variables directly (rather than invoking a set accessor and passing nilas the parameter), as illustrated in this example:

- (void)dealloc {
    [property release];
    [super dealloc];
}

If you are using the modern runtime and synthesizing the instance variable, however, you cannot access the instance variable directly, so you must invoke the accessor method:

- (void)dealloc {
    [self setProperty:nil];
    [super dealloc];
}

What is modern runtime in iOS application development exactly?

Upvotes: 5

Views: 836

Answers (1)

jscs
jscs

Reputation: 64002

It is possible to access the ivar directly, under the same name as the synthesized property. The @synthesize directive creates the ivar on your behalf if one does not already exist, and since that is a compiler directive, the ivar is available at compile-time. See "Runtime Difference" in the Declared Properties chapter of The Objective-C Programming Language. As Abizern noted in a comment, it's also possible to specify whatever name you like for the ivar: @synthesize coffee=tea; -- here, tea is the ivar and coffee the property.

To use the ivar, simply refer to it like any other variable, without using the dot syntax. The following is all perfectly legal and works as expected:

@interface Grisby : NSObject {}
@property (retain) NSObject * obj;
@end

@implementation Grisby

@synthesize obj;

- (void) dealloc {
    [obj release], obj = nil;
    [super dealloc];
}

- (id) init {
    self = [super init];
    if( !self ) return nil;

    obj = [NSObject new];

    return self;
}

- (NSObject *) obj {
    return [[obj retain] autorelease];
}

@end

The "modern runtime" was introduced with Mac OS X 10.5 (Leopard) as part of the transition to 64-bit. All versions of iOS use the modern runtime. Synthesized instance variables are a feature of the modern runtime, as noted in the link I provided above.

The other key difference, noted in "Runtime Versions and Platforms" of the Objective-C Runtime Programming Guide, is that instance variables are "non-fragile". There is a layer of indirection added to ivar storage and access which allows classes to add variables without affecting the storage of derived classes. It also presumably facilitates instance variable synthesis. Greg Parker has an explanation involving kittens, there's passing reference to it in Mike Ash's 2009 runtime writeup, and Bavarious here on SO has a swell post about ivar storage and class extensions.

You can see other things that changed, though without explanation, in the "Mac OS X Version 10.5 Delta" chapter of the Objective-C Runtime Reference.

Upvotes: 10

Related Questions