2

I want to declare @property without ivar. Is there any difference if I declare @property with strong, weak or assign ?

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
  • 1
    well... have you read the documentation about encapsulation maybe? here we come: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html – holex Feb 11 '16 at 12:29
  • I'm voting to close this question as off-topic because it seems the OP has shown zero interests to read the essential documentation which would answer their question immediately. – holex Feb 11 '16 at 12:34
  • 3
    I reopened this question because it was closed for the wrong reasons. The OP isn't asking what the difference is between strong and weak. The OP is asking if it matter whether it is strong or weak when the property isn't backed by an ivar. – rmaddy Feb 11 '16 at 14:13
  • @holex Sorry, I couldn't find the answer immediately. Where is it? – Eiko Feb 11 '16 at 14:18
  • @holex You seem to be the only one who can find the passage. Can you be more precise? That document is long. – Eiko Feb 12 '16 at 12:30
  • @Eiko, what if I told you that developing applications is not a piece of cake and it requires enormous knowledge and experience? – holex Feb 12 '16 at 12:49
  • @holex What's your point? Did you finally realize that the question was not addressed in the documentation that you linked? Being a developer for years and also having read tons of docs, I still think the question is a very valid one, and not covered by the docs. So I don't understand your link or your endeavor to close this question. Feel free to share your enormous knowledge and experience, that's what SO is about. – Eiko Feb 12 '16 at 13:18
  • @holex I think you're still missing the point of the question. It is not about the fundamental differences of strong, weak or assign. It is about their meaning when there is no underlying ivar. BTW: I perceive your words as rude and offensive and inappropriate. – Eiko Feb 12 '16 at 15:02
  • @Eiko, that is still the same question; and the answer is still under the link I have posted; I think your thinking is years behind the present, you will realise sooner or later that the doc is about describing _their meaning when there is no underlying ivar_. you'd get my attitude right as I don't like lazy people who doesn't want to read docs and understand it; in practice these kinda developers make the messiest code which becomes non-maintainable after a very short period of time and it's always awkward for every decent developer to explain the client, that they had actually paid for crap. – holex Feb 12 '16 at 15:16

2 Answers2

2

The @property declaration defines a contract with the user's of the property. If it is declared as weak, for example, then the user of the property knows that the passed object won't be retained.

This contract should be true whether the property is backed by an ivar or not.

In your case, the property should be strong, weak, or assign depending on your implementation of your property and the contract you wish to provide for the property.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

It depends of what type the property is. For example for primitive types like NSInteger you can't declare property with strong or weak attributes. Xcode will show you an error. So these declarations are invalid:

@property (nonatomic, strong) NSInteger max;
@property (nonatomic, weak) NSInteger max; 
@property (nonatomic, copy) NSInteger max;

because strong, weak and copy are attributes of object types and NSInteger isn't object type. It's primitive type. Thus, following declaration is valid:

@property (nonatomic, assign) NSInteger max;

Attributes assign, weak, strong, copy and unsafe_unretained specify memory management rules for ivar that is backed by property. So if you are not going to create ivar for property (suppose you want to provide your own getter/setter and property will not store any value) it doesn't matter what attribute you'll specify. But as I mentioned above you couldn't specify attributes of object types for primitive types.

curious
  • 666
  • 4
  • 13
  • This is not what the OP asked about. See my comment below the question. – rmaddy Feb 11 '16 at 19:20
  • @rmaddy what's wrong with this statement `Attributes assign, weak, strong, copy and unsafe_unretained specify memory management rules for ivar that is backed by property. So *if you are not going to create ivar* for property (suppose you want to provide your own getter/setter and property will not store any value) it doesn't matter what attribute you'll specify.` ? – curious Feb 11 '16 at 19:55