7

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.

Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
  • 1
    It honestly does not matter if outlets belonging to the view are declared strong or weak in ARC because the view will have a strong reference to the element. – Joe May 15 '13 at 12:56
  • @Joe yes i agree but still want to know, what are they – Raheel Sadiq May 15 '13 at 12:57
  • @lxt i seen this question before, it didn't clear me for this – Raheel Sadiq May 15 '13 at 13:00
  • 2
    Bear in mind that viewDidUnload is deprecated in iOS6 and no longer called. So if your deployment target is >=6.0 you can happily get rid of all your viewDidUnload code :) – Mike Pollard May 15 '13 at 13:07
  • 1
    @RaheelSadiq Apple advises that [`IBOutlet` references be weak](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6) (thus your second example), except for top level objects. While that doc is focused on NIBs, the same is true for storyboards. – Rob May 15 '13 at 14:23
  • 1
    @Rob yes I did, I mostly maintain a manual release/retain project so I'm bit rusty on some parts of ARC but I do recall that, thanks. – Joe May 15 '13 at 14:29

3 Answers3

7

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.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • 2
    +1 By the way, (and I know you know this already, but for the benefit of future readers), I hope readers don't interpret your comment as suggesting that making it `atomic` ensures thread safety. It doesn't. See [Properties Are Atomic by Default](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW37) in the _Programming with Objective-C_ guide. – Rob May 15 '13 at 14:27
  • For those who wonder what the exact definition of `IBOutlet` is (from the docs): Identifier used to identify a property so Interface Builder can synchronize the display and connection of outlets with Xcode. Insert this identifier immediately before the type in any declarations. – Kasper Munck Oct 16 '13 at 11:25
5

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

Community
  • 1
  • 1
Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
3

General rule of thumb, anything with an IBOutlet should be declared as weak.

Have a look at weak or strong for IBOutlet and other.

Community
  • 1
  • 1
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • Thanks for linking my answer ;) – Lorenzo B May 15 '13 at 13:10
  • 1
    +1 Anything (except top level objects, which is unusual that you ever have your own `IBOutlet` for that) should we weak. See [Managing the Lifetimes of Objects](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6) in Resource Programming Guide. – Rob May 15 '13 at 14:18