Skip to content

Instantly share code, notes, and snippets.

@jvlad
Created January 22, 2018 13:27
Show Gist options
  • Save jvlad/d0783634a804e649f81eee43bc6f05d6 to your computer and use it in GitHub Desktop.
Save jvlad/d0783634a804e649f81eee43bc6f05d6 to your computer and use it in GitHub Desktop.
Property-Attributes-in-Objective-C

Property Attributes in Objective-C

Storage

  1. strong
    it's default option implicitly applied if no any other option of the same group is specified
    means you have a reference to an object and you will keep that object alive. As long as you hold that reference to the object in that property, that object will not be deallocated and released back into memory.

  2. weak gives you a reference so you can still “talk” to that object, but you are not keeping it alive. If everyone else’s strong references go away, then that object is released.

Less used:

  1. assign assign is the keyword used for primitives. It is pretty easy to understand: If you do not have an object, you cannot use strong, because strong tells the compiler how to work with pointers. But if you have a primitive (i.e. int, float, bool, or something without the little asterisk after the type), then you use assign, and that makes it work with primitives.

  2. copy has been seen most commonly with strings. It works really well with all kinds of mutable objects. If you set a copy property, instead of just setting both references to the same object, what it actually does is it makes a copy of the object you are setting and then sets the property to that copy.


Atomicity

  1. atomic
    it's default option implicitly applied if no any other option of the same group is specified
    Is guaranteed that if you try to read from it, you will get back a valid value. It does not make any guarantees about what that value might be, but you will get back good data, not just junk memory.

  2. nonatomic
    Doesn't guarantee that you always get back something. If you try to read in the middle of a write, you could get back garbage data. But, on the other hand, you go a little bit faster.

If it is a property that you are accessing a lot, you may want to drop down to nonatomic to make sure that you are not incurring that speed penalty.


Access

  1. readwrite
    it's default option implicitly applied if no any other option of the same group is specified
    On the flip side, there is readwrite, which, of course, allows both read and write. That is the default; again, if you do not write anything, you have a readwrite property.

  2. readonly
    makes it so this property is read-only. There is no setter for it at all. One caveat is that it cannot be readonly to everyone else, but readwrite to me: if you say readonly, it is read-only to everybody, including you.


Credits

The original content is taken from 'TMI #1: Objective-C Property Attributes'
Fri Jan 19 17:09:17 MSK 2018
https://academy.realm.io/posts/tmi-objective-c-property-attributes/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment