What is __weak?


#1

I understand what __weak does. I just don’t understand what it is? What is this thing with two underscores? What do you call it?


#2

I have a similar question, so I do not want to open a new thread :

@interface Asset : NSObject
{
   ....
    __weak Employee *holder;
   ....
}
....
@property (weak) Employee *holder;
....
@end

Isn’t it sufficient to use “(weak)” with the property declaration here ? IMHO __weak is optional, but still good programming
style to clarify that this is no strong (default) reference.

cu
Vertex


#3

@Glibny the keywords such as __weak and __strong are called “storage qualifiers”. As to why they’re prepended with the double underscore, I’m uncertain. My best guess is to maintain compatibility with existing code. It’d stink if you’d written an app with variable or function names that Apple ended up turning into reserved keywords, breaking your app the next time you built it. Since words prepended and/or postpended by double underscores are typically reserved for the compiler, it’s unlikely that anyone had conflicting code when these keywords were defined.

@Vertex a property declaration always requires a storage qualifier for an object pointer, but an explicit ivar declaration is happy to assume __strong ownership if you leave out the storage qualifier. Also, the qualifiers used with the property and the explicit ivar declaration must match, whether or not you explicitly add a storage qualifier to your ivar declaration.

That is, the example you’ve pasted above is perfectly fine, but

@interface Asset : NSObject
{
   ....
    Employee *holder;
   ....
}
....
@property (weak) Employee *holder;
....
@end

would not compile. The explicit ivar declaration (the bit in the { … } scope) would assume __strong for *holder, which is a mismatch to the explicitly weak property declaration.

Does that make sense?