Search code examples
objective-cios7automatic-ref-counting

Why are delegate properties given assign but commented weak in iOS7.1 SDK?


Under ARC,
assign implies __unsafe_unretained ownership.
weak implies __weak ownership.

enter image description here

Why are delegate properties given assign but commented weak in iOS7.1 SDK?


Solution

  • History. Prior to ARC, you used assign for "weak" (non-retaining) references. "Weak" just meant "I won't call retain on it" which translates into assign.

    Before properties, we just knew that things called delegate didn't call retain by convention. Other non-retaining properties needed some kind of documentation.

    There have been ongoing efforts to clean up the iOS SDK to conform to more modern conventions, but there are still places that use older approaches. I'm not exactly certain why they used id here, for instance, but it may be because of old habits around formal and informal protocols. Before ObjC 2, you couldn't have optional methods in a protocol, so you generally used informal protocols (i.e. you passed a bare id, and checked that it responded to what you needed). This style of commenting the protocol was often used in that case. (Not saying that's exactly the reason here; I believe UIAlertViewController post-dates ObjC 2. Just an example of things that take a long time to work their way out of the SDK.)