Are @property's necessary for Interface Builde

2019-05-05 23:30发布

In my UIViewController subclass, I have 3 UIView's with each a @property as an IBOutlet. I do not use these properties at all in my code. The views get instantiated as soon as the view controller is created and they are deallocated when the view controller is deallocated.

I was thinking; can't I just remove the @property's? I did, and I could still connect my instance variables (with IBOutlet) in Interface Builder.

So my question now is; is there any use for properties in combination with Interface Builder, or is it OK to leave them out? Is it required for some memory management or something? Or are they really just for use in your own code?

And if I do leave them out, do I still need to release them in dealloc?

5条回答
闹够了就滚
2楼-- · 2019-05-05 23:33

There are two reasons to use property for views:

  • Easier/safer memory managment (Really good reason)
  • Exposing your interface to other clasess (usually bad coding practice)

I always use properties just to make sure that my code doesn't crash if I forget to nil the IBOutlets in viewDidUnload. After all it isn't that hard to just write 2 lines of code.

If you would like to save some typing don't declare ivars at all. The code below works just fine:

@interface MyController : UIController {
}
@property (nonatomic, retain) IBOutlet UIImageView* myImage;
@end

@implementation MyController
@synthesize myImage;

- (void) dealloc() 
{ 
   self.myImage = nil;
   [super dealloc];
}    

- (void) viewDidUnload()
{ 
   self.myImage = nil;
   [super viewDidUnload];
}
@end
查看更多
叼着烟拽天下
3楼-- · 2019-05-05 23:35

yes you can:

@interface SomeClass : UIViewController {
    IBOutlet UILabel *myLabel;
}

@end

@property is only for defining memory management and getter and setter, read-write ability etc.

查看更多
再贱就再见
4楼-- · 2019-05-05 23:41

If you're not planning on manipulating or accessing information about your views from other view controllers or objects and planning on using them internally (in owning view controller) you don't need @property statments at all. Just define them as IBOutlet instance variables in your view controller's interface.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-05-05 23:41

Interface Builder or the NIB loader doesn’t depend on the properties to be there. If the connected variables can be accessed via KVC the nib loader can make the connections.

But you really should consider using synthesized properties instead of hand-written accessors. This is much less error-prone because it doesn’t require as much boilerplate code. The less code you have to write the better.

查看更多
做自己的国王
6楼-- · 2019-05-05 23:58

They're not necessary, but they're strongly encouraged for the simple fact that they clarify memory management.

For example, if you declare an outlet via the ivar, then what is its retain policy? Is it retained? Is it autoreleased? Are you the owner? Does someone else also own it? There is a lot of ambiguity there (especially with those accursed top level objects, which behave differently on the Mac than on the iPhone).

On the other hand, if you declare the outlet via a property, there is no ambiguity, because the memory management policy is directly stated in the declaration. In addition with the presence of the property, the nib unarchiving will see the setter and use that, thereby ensuring that nothing strange is going on with transferring object ownership, etc.

In a nutshell, you can declare outlets without using @property (which we all had to do before they were introduced in 10.5), but there's no good reason to not use them. They really make the code a lot clearer as to what exactly is going on.

For more info on the absurdity of nib object memory management, check out this page in the documentation.

查看更多
登录 后发表回答