0

Properties are now synthesized by default. The compiler will use the property name plus the underscore prefix to create the ivar. And instance variables have strong storage type by default, so it means property is by default strong. So why does property require a strong keyword (seen a lot many people specify it explicitly). Are there be cases where property is weak?
Sorry, if it seems to be a noob question.

Edit: IBOutlet, delegates should be weak, any other property apart from that which should be weak.

Amit
  • 125
  • 8
  • as apple docs say below, it is infact strong. https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW30 – Amit Nov 12 '14 at 16:14

4 Answers4

2

Properties are strong and atomic by default.

Typing it in explicitly is more for readability purposes than anything else.

There are times when you should use weak properties. Delegates, IBOutlets, etc...

But you have to declare these explicitly.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 2
    Properties are strong and **atomic** by default. To quote the docs: – Duncan C Nov 12 '14 at 15:54
  • @DuncanC oops, my mistake. lol, fixed. I knew it was one way or the other, lol. – Fogmeister Nov 12 '14 at 15:54
  • I was trying to paste a quote from the docs into the comment, but it had "@" compiler directives in it, and SO thought it was quote tags. By the time I got rid of that stuff, it was no longer recognizable as a quote from the docs. I wish there was better formatting on comments. – Duncan C Nov 12 '14 at 15:58
  • @DuncanC agree 100% it's hard to put anything constructive into a comment. I usually end up editing my answer if the comments are against my answers. – Fogmeister Nov 12 '14 at 15:59
1

I would also add to the other answers that block properties require you to specify ownership explicitly.
Usually you need copy for blocks (strong works correctly under ARC but you must use copy under MRC)

Bartek Chlebek
  • 1,665
  • 1
  • 16
  • 23
  • 1
    How does this (copy vs strong) relate to the question? "Are there be cases where property is weak? " – Mark McCorkle Nov 12 '14 at 16:23
  • Yes, with ARC you can use `strong` for block variables, but [Apple advises](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html) `copy` regardless: "You should specify `copy` as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn’t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it’s best practice for the property attribute to show the resultant behavior." – Rob Nov 12 '14 at 16:24
  • @DCGoD Agreed. This probably should have been comment, not answer. And while this is correct, I think it was intended as a clarifying point to the other answers and, as such, this probably shouldn't be the accepted answer (no offense, Bartek). – Rob Nov 12 '14 at 16:26
  • 1
    @Rob oh no, I agree :) As you noticed and I mentioned at the beginning, my answer was supposed to supplement the others rather than be a single explanation. And to answer how this copy vs strong relates to the original question, well one answer mentions that `strong` is the default one, so I wanted to point out, that blocks do not have this implicit `strong` and to be careful in `MRC` code with typing `strong` as it would be incorrect. And it may explain why the OP has seen `strong` a lot in many places. – Bartek Chlebek Nov 12 '14 at 16:37
0

Generally weak references are seen automatically from IBOutlets since the view itself holds a strong reference. There are many other use cases. This is to prevent retention cycles.

http://www.quora.com/What-is-the-difference-between-strong-retain-nonatomic-etc-in-objective-C-iOS-property

Avoiding circular retention using ARC (strong/weak), learning some basics

EDIT: More information can be found here and should be fully understood before looking specifically for examples of weak references besides IBOutlets and delegates. Understanding why each of those are generally weak references would lead to an explanation of what should or shouldn't be weak.

Differences between strong and weak in Objective-C

Community
  • 1
  • 1
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
0

Views and Delegates should almost always be weak.

Views should only have a strong reference from superviews. Delegates should just have to care that their work was done.

Krys Jurgowski
  • 2,871
  • 17
  • 25
  • Agreed, though to split hairs, the top level view is `strong` (that's generally done for you) and most outlets (which is what we tend to spend our time with) are `weak`. From the [Resource Programming Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html): "Outlets should generally be `weak`, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be `strong`." – Rob Nov 12 '14 at 16:36