Xcode and nested classes. Bad story

xcode

(Dmitry Lobanov) #1

Well, I have an exercise with nested classes.

Suppose "architecture" - a domain-driven Developer module that contains developer settings for an app.

However, this module is not extracted into target.

Next, follow nesting:

Developer module has settings. Settings can be presented in ViewModel that could create ViewController which has several cells to modify settings.

Next, try to extended nested class which has 3 or 4 nesting like Cell or ViewController.

extension Developer.Settings.ViewModel.ViewController.CellWithSwitch {
// Some methods.
func example() {
self // <- self is error type here.
}
}


(Jeremy Pereira) #2

So I did this in a Playground (Xcode 10, Swift 4.2)

struct Developer
{
    struct Settings
    {
        struct ViewModel
        {
            struct ViewController
            {
                struct CellWithSwitch
                {
                    var a: String = "Hi"
                }
            }
        }
    }
}
extension Developer.Settings.ViewModel.ViewController.CellWithSwitch
{
    func example()
    {
        let y = self.a
        print("\(y)") // <- self is error type here.
    }
}

let x = Developer.Settings.ViewModel.ViewController.CellWithSwitch()
x.example()

No error and "Hi" was printed as expected.


(Dmitry Lobanov) #3

Try to put them in separate files.


(Jeremy Pereira) #4

When I try to compile your code I get an error on the definition of Developer.Settings.ViewModel unless I move it into the same file as the definition of Developer.Settings. It looks like a compiler bug to me.


(Jordan Rose) #5

This sounds like SR-631. Can you try with a snapshot from the "master" branch on https://swift.org/download ?