0

I’ve been trying to use Swift outlet collections in a MacOS project for some time, and have only just learned this isn’t currently possible. I’m still puzzled why not, but presuming this is amended at some point, I have another concern.

I understand that outlets should generally be weak, except for the “root” outlet, to prevent retain cycles:

@IBOutlet weak var someButton: NSButton!

But the examples I’ve found for collection syntax don’t include the weak modifier, and I haven’t found a place to put it that (1) makes sense, and (2) doesn’t get a red flag:

@IBOutlet var severalButtons: [NSButton]!

So, am I missing something obvious? Could I be sure such an outlet collection wouldn’t create a retain cycle? (Presuming they’re ever allowed?)

Jeff J
  • 139
  • 8

1 Answers1

2

An IBOutletCollection, unlike an IBAction or an IBOutlet, takes a class name as an argument. As a top-level object, an IBOutletCollection should be declared strong. This is explained in more detail here.

Therefore, due to this restriction, an IBOutletCollection has the potential to create a retain cycle.

Alexander MacLeod
  • 2,026
  • 3
  • 14
  • 22
  • I think we’re on the same page: I do understand the collection itself must have a strong reference, and you’re confirming my suspicion that the strong references within the collection array could lead to problems if I’m not careful. But in my case the collection would be owned (it’s hypothetical at the moment, because MacOS) by the interface root object. So ownership within the interface is no longer a simple tree, but a directed acyclic graph. But I think I’d be ok, because ownership is still “acyclic.” – Jeff J Apr 15 '17 at 03:20
  • @JeffJ Glad you understand. What answer then are you looking for? – Alexander MacLeod Apr 15 '17 at 06:48
  • I think you’ve answered me as far as that’s possible at the moment. I was worried that I was missing something because the Swift syntax for a collection doesn’t seem to allow for the possibility of setting up weak outlet references, which still seems odd. Unless you’d care to take a crack at why IB doesn’t seem to recognize the presence of collections in MacOS projects, while I’m told it does just fine in iOS projects? Otherwise, thanks. – Jeff J Apr 16 '17 at 00:43