Reputation: 18217
I suppose the answer to this will be "no", but I want to make sure. Is there a convenient way to have a lot of object attributes in Objective C? If I use properties, I have to write four lines just to have one attribute:
Foo.h:
@interface Foo : NSObject {
NSString *str; // first line
}
@property (copy) NSString *str; // second line
Foo.m:
@implementation Foo
@synthesize str; // third line
(void) dealloc {
[str release]; // fourth line
...
}
On the other hand, without properties every time I set str I have to release the previous one and still need to release on dealloc as well. Is there a more convenient way or perhaps an XCode4 shortcut to declare properties?
Upvotes: 0
Views: 62
Reputation:
If you’re willing to use third party scripts/services, David Hoerl has recently published one that helps with managing instance variables and declared properties.
From his announcement:
Announcing Version 1.0 of the PropertyMaster Xcode script (and Automator Service) to assist iOS (and to a lesser extent, Cocoa) developers convert existing ivar based code to all properties, and to maintain the property/viewDidAlloc/dealloc relationships.
Once installed as an Xcode 3 script (or as a Service for Xcode 4), you can select interface files containing both ivars and properties, and convert any ivars to properties, allowing you to subsequently delete all ivars. It pastes one large text blob on the clipboard, you can paste it back into both the interface and implementation file and ordered and classified synthesize lines, a viewDidUnload and dealloc method. It also lets you select and process a class extension containing properties, and regenerates a merged blob.
In addition, it has a highly customizable system to identify weak ivars, and to produce final "close" statements prior to a release (ie, foo.delegate = nil, [foo release];)
If the URL on the announcement doesn’t work, try this one.
Upvotes: 1
Reputation: 92335
You could save the "first line", that is the variable definition (but then you'd have to change the "fourth line" to self.str = nil;
). Objective-C supports generating an implicit variable on demand for your property, but this feature isn't available on all platforms: it's available on iOS and Mac 64-bit, but not Mac 32-bit.
Upvotes: 1