Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBOutlets strong or weak [duplicate]

Outlets can be created like this

@interface SearchViewController : UIViewController<UISearchBarDelegate> {    
    IBOutlet UIView *viewSearchBar;
    IBOutlet UIScrollView *scrollVieww;
    IBOutlet UILabel *lblName;
}

and also like this

@interface SearchViewController : UIViewController<UISearchBarDelegate> {

}

@property(nonatomic, weak) IBOutlet UIScrollView *scrollVieww;
@property(nonatomic, weak) IBOutlet UIView *viewSearchBar;
@property(nonatomic, weak) IBOutlet UILabel *lblName;

@end

I know the nonatomic/atomic strong/weak in ARC, but in the first example what are they? strong, weak, nonatomic or atomic.

Please explain or link me to some detail.

like image 317
Raheel Sadiq Avatar asked May 15 '13 12:05

Raheel Sadiq


People also ask

Should IBOutlets be weak or strong?

The official answer from Apple is that IBOutlets should be strong. The only case when an IBOutlet should be weak is to avoid a retain cycle. A strong reference cycle can result in memory leaks and app crashes.

Why are IBOutlets forced unwrapped?

It's only after the view controller is initialized that it loads its view. This also means that any outlets declared in the view controller class don't have a value immediately after the view controller's initialization. That's why an outlet is always declared as an (implicitly unwrapped) optional.

Why We Use weak in IBOutlet Swift?

@IBOutlet makes Interface Builder recognize the outlet. private ensures the outlet isn't accessed outside the current class. weak is used because in most situations the owner of the outlet isn't the same as the owner of the view. For example, a view controller doesn't own someLabel - the view controller's view does.

What is the difference between IBOutlet and IBAction?

An IBOutlet is for hooking up a property to a view when designing your XIB. An IBAction is for hooking a method (action) up to a view when designing your XIB. An IBOutlet lets you reference the view from your controller code.


2 Answers

Instance variables under ARC are strong by default. And they are neither atomic nor nonatomic, since they are just instance variables and not accessor methods. The atomic/nonatomic flags are related to multi threading. They specify whether or not the accessor methods should be atomic. When an accessor is atomic, the execution can't change to an other thread in the middle of the accessor method. When it's nonatomic, there is no such restriction.

Note: IBOutlet is a typedef of nothing. It's just a flag for Interface Builder and has no memory related functions.

like image 84
DrummerB Avatar answered Sep 22 '22 00:09

DrummerB


Variables are __strong by default under ARC so:

IBOutlet UIView *viewSearchBar;

is the same as

IBOutlet __strong UIView *viewSearchBar;

With regard to the recommended way to deal with IBOutlets under ARC see: the answer to this

like image 31
Mike Pollard Avatar answered Sep 23 '22 00:09

Mike Pollard