Kodeco Forums

Video Tutorial: Intermediate Core Data Part 1: Core Data Stack

Learn about the classes that make up Core Data and assemble these objects together into a reusable class.

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3815-intermediate-core-data/lessons/2

Hi Greg! Awesome tutorial!

There is one thing I do not understand: When you pass down the coreDataStack from appDelegate to viewControllers. There is this selector of the top viewController:

if top.respondsToSelector("setCoreDataStack:") {
            top.performSelector("setCoreDataStack:", withObject: coreDataStack)

I am wondering where does this method “setCoreDataStack:” come from? And what does it do specifically? Is it generated automatically by swift when we set the property through segues?

And also, what does this code do?
for child in tab.viewControllers ?? []
Is it nil coalescing? Because this seems a little weird to me. I know format like A = B ?? C, but I have never seen one like that. My understanding is that if tab.viewControllers is nil, then make it an empty array?

Thank you so much. I am still new to programming, so hope you don’t mind.

1 Like

Hi @chenglu, I hope these explanations make sense:

The respondsToSelector is an Objective-C runtime thing; maybe it’s relatively rare in Swift. For properties on classes that descend from NSObject (as view controllers do), there are actually accessor methods created for you. So for a property coreDataStack you get a getter method coreDataStack and setter setCoreDataStack:. That’s where the method is coming from: it comes “free” with the property.

For the second question, that is indeed nil coalescing and you have the meaning correct! You can think of the longer version with the ternary operator:

for child in (tab.viewControllers != nil ? tab.viewControllers : [])

But the nil coalescing is much more concise.

Thanks for the questions! Again, I hope this makes sense and I’ve cleared up your concerns.

Hi @gregheo, why respondsToSelector and not a singleton class?

1 Like