<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">moc is declared inside the view controller class, which is a subclass of <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 14px;" class="">NSViewController</span> which is an Objective-C class. So I believe the scope is @objc</div><div class=""><br class=""></div><div class="">In any case, I tried to prefix the declaration with “@objc”, but this is has no visible effect.</div><div class=""><br class=""></div><div class="">That being said, prefixing its declaration with “dynamic” sort of fixes the problem: Interface Builder still complains as described, but the program now works fine at runtime.</div><div class=""><br class=""></div><div class="">I have a hard time understanding clearly why: for bindings to work, the bound properties need to be KVO-compliant. But wouldn’t the @objc scope already do that?</div><div class=""><br class=""></div><div class="">What is the exact effect of the dynamic annotation?</div><div class=""><br class=""></div><div class="">JD</div><div class=""><br class=""></div><div class="">PS: I made a zip archive of the project available at <font color="#646464" face="Arial, Helvetica, sans-serif" class=""><span style="font-size: 11px; text-align: center;" class=""><b class=""><a href="http://dl.free.fr/pWkoKQLOc" class="">http://dl.free.fr/pWkoKQLOc</a></b></span></font>. There is also an equivalent Objective-C project that works fine.</div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 17 Jan 2017, at 17:43, David Sweeris <<a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Is `moc` declared in a non-"@objc" scope? I think there's a "@IBsomethingerather" that you have to put in front of those to let Interface Builder see them or something. Sorry I can't be more definitive, but I'm not in front of my computer and can't double-check.<br class=""><br class="">HTH<br class="">- Dave Sweeris <br class=""><br class="">Sent from my iPhone<br class=""><br class=""><blockquote type="cite" class="">On Jan 16, 2017, at 10:11, Jean-Denis Muys via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class="">Hi,<br class=""><br class="">I am exploring the use of Swift for a Mac Cocoa application - using Xcode 8.2.1, and it seems I hit a roadblock regarding Cocoa bindings.<br class=""><br class="">In this toy Core Data document-based project, I added an NSArrayController in the StoryBoard. I need to bind it a NSManagedObjectContext so that my user interface works by itself. I followed the solution outlined in Technical Q&A QA1871 (<a href="https://developer.apple.com/library/content/qa/qa1871/_index.html" class="">https://developer.apple.com/library/content/qa/qa1871/_index.html</a>).<br class=""><br class="">So I want to add a property of type ManagedObjectContext to my ViewController class. <br class=""><br class="">I naturally declared it as an optional:<br class=""><br class="">var moc: ManagedObjectContext?<br class=""><br class="">But when I enter the property name in the bindings inspector of InterfaceBuilder, it complains: there is a red exclamation mark, and hovering over it pops up this error message:<br class=""><br class="">“The Managed Object Context binding expects to be bound to an object of type NSObject, but mac is of type ManagedObjectContext?”<br class=""><br class="">And it fails at run time too.<br class=""><br class="">changing the type to ManagedObjectContext! doesn’t help: IB complains in exactly the same way.<br class=""><br class="">changing the type to a non optional ManagedObjectContext silences the IB error, but now my ViewController class doesn’t compile anymore. The error I get is:<br class=""><br class="">class ViewController has no initialiser <br class=""><br class="">I completely understand this error message. And I can add an initialiser. But I get this new error message:<br class=""><br class="">property ‘self.moc’ not initialised at super.init call.<br class=""><br class="">I understand that one too, but what can I do? At initialiser-time, the managedObjectContext is not yet known. In Objective-C I would set it to nil, which I cannot do since the property is not an optional any more.<br class=""><br class="">Do I really need to allocate a dummy sentinel ManagedObjectContext, just to make the compiler happy?<br class=""><br class="">This would be ugly as hell, far worse than the nil value we use in Objective-C. Swift in that case, would not be safer, but less safe than Obj-C.<br class=""><br class="">I find this idea repulsive. Or did I miss something?<br class=""><br class="">Or is Swift fundamentally incompatible with Cocoa bindings? That would be a pity too.<br class=""><br class="">Thanks,<br class=""><br class="">Jean-Denis<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></blockquote></div></div></blockquote></div><br class=""></div></body></html>