@synthesize IBOutlet Property

I am new to Objective-C, and I read Alasder Allan's “Programming the iPhone”. While reading, I found this code:

@interface RootController : UIViewController <UITableViewDataSource, UITableViewDelegate> { UITableView *tableView; NSMutableArray *cities; } // warning: remember this tableView @property (nonatomic, retain) IBOutlet UITableView *tableView; 

Relative implementation begins as follows:

 @implementation RootController @synthesize tableView; 

Now: I found out that @synthesize is a kind of shortcut to avoid boring getters and setters.

But I have a question:

  • tableView is never explicitly named in the implementation code, but dealloc frees it;
  • If it is never explicitly called, why @synthesize?

Is it mandatory to synthesize IBOutlets?

+8
ios objective-c iphone
source share
5 answers

From Nib Object Memory Management ,

When the nib file is loaded and the output points are set, the nib-load mechanism always uses access methods if present (both on Mac OS X and iOS). Therefore, no matter what platform you develop, you should usually declare exits using the declared Objective-C properties function.

For iOS, you should use:

@property (non-atomic, persistent) IBOutlet UIUserInterfaceElementClass * anOutlet;

Then you must either synthesize the appropriate access methods, or implement them according to the declaration, and (on iOS) release the corresponding variable in dealloc.

+7
source share

in the implementation code, tableView is never explicitly called, but dealloc frees it;

This is because when you assign a tableView value, your controller saves it and it will need to free it when it gets dealloc'd. Remember that @properties declared in the interface are publicly available. In your case, it is the tableView that you declare as an IBOutlet that is initialized using the loadView controller loadView , using the connections that you define in the Interface Builder between the file owner and the UITableView.

if it will never be called explicitly, why @synthesize?

You need to provide an accessory for all announced @properties. They can be @synthesized, or you can write your own.

Is it mandatory to synthesize IBOutlets?

No, but it’s more convenient. The rule executed by the compiler is that @properties must have appropriate accessors (synthesized or not) in the implementation.

+3
source share

For reference: Xcode 4.4 and LLVM Compiler 4.0 are no longer required in the @synthesize directive, since it will be provided by default for @properties defined in the interface.

+2
source share

If you type

 @property (nonatomic, retain) IBOutlet UITableView *tableView; 

tell the compiler: “Listen, there will be a getter and a setter. Use them if necessary!” And he will use them when downloading the nib.

So you need to implement getter and setter, otherwise the compiler will complain.

+1
source share

The IBoutlet pseudo-type is just a marker, so InterfaceBuilder “knows” that the file class has a handle / exit for the UITableView instance.

When compiling, the IBOutlet is deleted by the preprocessor (InterfaceBuilder parses (looks) the source files). It is similar to IBAction: it is replaced by an invalid preprocessor.

However, you can use the link to the mentioned instance for programmatic programming (like adding / changing UITableView values)

0
source share

All Articles