开发者

Xcode confusion - property and synthesize, retain?

I see the property and synthesize being used without "declaring the variable" first.. and I'm a bit confused about what properties to use. I want to alloc and init my viewController in AppDelegate and then be sure it's there for the remainder of the run. Clearly I want retain-property?.. however.. since alloc returns the viewController with retain 开发者_StackOverflow中文版count 1, it seams a lot smarter to just use leave the retain-property out. No other class will use my setter, so I don't care then?

Ex.

in AppDelegate.h:

    @propert(nonatomic,retain) MyViewController *myViewController;

in AppDelegate.m:

    @synthesize myViewController = _myViewController;
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.myViewController = [[[EventDataController alloc] init] autorelease];
        [self.window makeKeyAndVisible];
        return YES;
    }

or..

in AppDelegate.h:

    @propert(nonatomic) MyViewController *myViewController;

in AppDelegate.m:

    @synthesize myViewController = _myViewController;
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.myViewController = [[EventDataController alloc] init];
        [self.window makeKeyAndVisible];
        return YES;
    }

Set me straight, please.


You don't need to define instance variables for properties anymore - @synthesize does that for you.

As for your other question - you can do it either way, but don't forget to release it in your -dealloc. I like (nonatomic, retain), because it is very clear and easy to use/understand. All you do is assign and it does all the rest:

self.myViewController = [[[ViewController alloc] init] autorelease];

In your -dealloc:

self.myViewController = nil;

In the case of manually releasing you might want to forget about properties at all and only use instance variables like this:

@interface MyClass: NSObject
{
  ViewController* _myViewController;
}
@end

In your implementation:

_myViewController = [[ViewController alloc] init];

In your -dealloc:

[_myViewController release];
_myViewController = nil;

Note that the last assignment of nil might be unnecessary, but I've had too many hard-to-track bugs with this (and that's why I love retained properties - all you need to do is set them to nil).

I try to always go with retained properties as this saves me some brain cycles and I don't care about the nanosecond I could save for the CPU otherwise.


I would go with the first option.

Why can't you explicitly release myViewController when you're done with it?

I wouldn't make assumptions like "this class will never be used by anybody else".


of the options, i prefer declaring the property with retain. this is documentation for your sake, and it can save you from silly mistakes because of tools such as static analysis. since your class owns the controller, it should be declared that it holds a reference to it, regardless of implementation details. deviation from conventional idioms is a good way to introduce bugs. you should also declare it as readonly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜