18

I downloaded the Xcode 4.2 developer preview version and I created a cocoa application. But I found a very weird syntax in the delegate class:

@property (strong) IBOutlet NSWindow *window;

What does this mean? And the compiler can't even compile it.

Thanks in advance!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
zs2020
  • 53,766
  • 29
  • 154
  • 219
  • 2
    I can't tell you because of the NDA, but, as you're in the Dev Program, read the Programming with ARC Release Notes. As an aside - this is why we can't have nice things. – Abizern Jul 07 '11 at 15:51
  • Wow -- I can't find anything online about `strong`. It seems to be related to IBOutlets and in particular whether they are strong or weak references, but that's about all I got. What error message are you getting from the compiler? – Chris Gregg Jul 07 '11 at 15:57
  • @Chris Gregg: Not IBOutlets specifically; it's for properties. The (NDA'd) WWDC 2011 session that introduces ARC covers it; it's probably in at least some of the NDA'd documentation, too. – Peter Hosey Jul 07 '11 at 16:01
  • @Peter Hosey -- thanks; just read the comment about NDA and realized it was something new, too. Cheers. – Chris Gregg Jul 07 '11 at 16:02
  • 4
    ARC -- automatic reference counting -- is not under NDA, though the rest of iOS 5 and Lion are. – bbum Jul 07 '11 at 16:24
  • @bbum - but the reference I mentioned in my comment is. – Abizern Jul 07 '11 at 16:45
  • 3
    The ARC documentation is public; http://clang.llvm.org/docs/AutomaticReferenceCounting.html. The reading of it, though, is rather dense (and the discussion of @property is by implication). `@property(strong)` and `__strong` are effectively synonymous. The presence of `IBOutlet` is orthogonal. – bbum Jul 07 '11 at 17:18

2 Answers2

20

It indicates that this property is a strong relationship—an ownership. It's ARC's version of the retain keyword in the same context.

And the compiler can't even compile it.

It's valid ARC code, so if your tools support ARC, they certainly should be able to compile it.

Make sure that you're using Xcode 4.2 or later, and that you have the project's compiler choice set to Clang (“Apple LLVM Compiler”).

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Tangential question: How is ARC "automatic" if the developer still needs to worry about "strong" and "weak" references? It seems that all they've done is replace `retain` and `release` with something even less intuitive. – aroth Jan 16 '12 at 00:47
  • 2
    @aroth: Because all you need to do is declare whether the property is strong or weak (or copying). You don't need to do anything else thereafter: you don't need to release the object in dealloc, you don't need to make sure you retain (if strong) when assigning to the instance variable, and you can have weak references that automatically get set to nil when the referenced object dies. – Peter Hosey Jan 16 '12 at 01:20
  • @aroth: You also don't need to worry about whether you use, say, `stringWithFormat:` or `alloc` and `initWithFormat:`; ARC upholds the memory management rules for you, with the net effect that neither one will leak or cause a crash. – Peter Hosey Jan 16 '12 at 02:09
  • Fair enough, I suppose the answer is in its name as well. It automates the _reference counting_, but that still falls a bit short of being truly automatic memory management (ala Java, ActionScript, etc.) in my opinion. Maybe it wasn't really intended to be anything of that sort? – aroth Jan 16 '12 at 04:46
  • @aroth: What alternative are you comparing ARC to? What are you expecting of it? – Peter Hosey Jan 16 '12 at 04:56
  • I suppose I'm comparing it to the automatic memory management present in the smattering of other languages I've worked with that have the feature. For instance Java, C#, ActionScript, PHP, Python, etc.. My hope is that it would be comparably powerful to the features of those memory management schemes, in that you never have to explicitly declare anything with respect to who owns a reference or how long it should be kept. It sounds like either ARC is not quite there yet, or it is actually not intended to solve that specific problem? – aroth Jan 16 '12 at 05:11
  • @aroth: It's coming from the experience of having tried to solve that specific problem. Apple introduced garbage collection in Mac OS X 10.5, but it never took off, both because when garbage got collected was unpredictable (I've seen this demonstrated very well by Xcode itself, which is GC'd) and because it doesn't interoperate with older, reference-counted code. Eliminating cycles and powering one or two nice instruments in Instruments were about the only upsides it provided. – Peter Hosey Jan 16 '12 at 06:09
  • @aroth: If you have an Apple developer account (which is free), I recommend watching the video of the WWDC 2011 session titled “Introducing Automatic Reference Counting”: https://developer.apple.com/videos/wwdc/2011/ It gives a good overview of what it does, what it doesn't do, and why. – Peter Hosey Jan 16 '12 at 06:15
3

Strong refers to the automatic reference counting (ARC) that is arriving in the new LLVM compiler, part of Xcode 4.2 when it's released. Presently, Xcode 4.2 preview is still under NDA - post any questions to the developer forums by logging in to your developer account.

See this good weblog post.

petert
  • 6,672
  • 3
  • 38
  • 46