2

I saw the following line:

@property (nonatomic, strong) dispatch_queue_t filterMainQueue;

Why is declared an instance of dispatch_queue_t, which is not an object, as strong?

joan
  • 2,407
  • 4
  • 29
  • 35

1 Answers1

7

This is totally a good practice. I want to notice that since OS X Mountain Lion and iOS 6.0 all GCD and XPC objects in iOS/ OS X runtimes are now treated as Objective-C objects by ARC and they are not primitives now so they will be memory-managed the same way as usual Objective-C objects. That's why from now you should declare them as strong.

You can check this yourself in object.h :

#define DISPATCH_DECL(name) OS_OBJECT_DECL_SUBCLASS(name, dispatch_object)

This fact is also stated in the docs: iOS 6 and later—Dispatch objects (including queues) are Objective-C objects, and are retained and released automatically. OS X 10.8 and later—Dispatch objects (including queues) are Objective-C objects, and are retained and released automatically. Earlier versions—Dispatch objects are custom objects. You must handle the reference counting manually

And also from the comments in object.h:

 * By default, dispatch objects are declared as Objective-C types when building
 * with an Objective-C compiler. This allows them to participate in ARC, in RR
 * management by the Blocks runtime and in leaks checking by the static
 * analyzer, and enables them to be added to Cocoa collections.
 * See <os/object.h> for details.
Oleksandr Karaberov
  • 12,573
  • 10
  • 43
  • 70
  • Hi, your claim is based on the name of the `define` `OS_OBJECT_DECL_SUBCLASS`? Because it is a subclass, we can assume that they are now objects? What would be `name` and `dispatch_object` in this case? Thanks in advance. – Unheilig Jun 26 '14 at 11:54
  • @Unheilig My claim not only based on this. Please see my edits. It is also stated in docs so it is more authoritative fact that my own `object.h` research – Oleksandr Karaberov Jun 26 '14 at 11:56
  • I get a `property with 'retain (or strong)' attribute must be of object type` error even though I'm using OS X 10.10 SDK. – Rivera Dec 01 '14 at 05:55
  • @Rivera This behaviour is controlled by the `OS_OBJECT_HAVE_OBJC_SUPPORT` macro, which is defined in header. Make sure it is set to 1. And more important: check if your deployment target is actually set to iOS 6.0 or higher. – Oleksandr Karaberov Dec 01 '14 at 08:22
  • It was for a library, so the deployment target could be either iOS 5+ or 6+. I ended with this solution http://stackoverflow.com/a/24846028/1049134 Thanks! – Rivera Dec 02 '14 at 01:42