Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode sees only some of similar extensions of a nested class written in separate files

I'm trying to keep my code as readable as it possible by keeping methods and files as short as I can and using nested classes for namespacing. It works fine except some really strange moment.

I have some class used for namespacing.

class Space { }

All classes used within that one are implemented in their own files as extensions.

extension Space {
    class SomeClass {
       // implementation
    }
}

One of those SomeClasses have a number of quite sophisticated initialisers, so I have split them up to their own files as well and implemented it as follows:

extension Space.SomeClass {
    convenience init(fromSomeSource source: SourceClass) {
        self.init()
        // other implementation
    }
}

The problem is that some of those files works just fine, but some of them throwing 'SomeClass' is not a member type of 'Space' and I don't know why.

All of them are pretty similar. The only difference is implementation of an initialiser itself. All files are held in the same place and I have no idea why some of them works fine and some not.

I tried to move code from not working files into files that works fine and that works – Xcode agrees to see the code and said nothing against it. But when the very same code lies in its own file – Xcode or compiler doesn't want to understand that SomeClass is really a member of Space.

I tried to clean the build, including manual dumping of ~/Library/Developer/Xcode/DerivedData folder. Nothing helps.

Surely I can put it all in a single file and it will work fine, but what the reason why it so picky in my case?


I've tried to create a new file and move there all contents from one of the bad ones. It works, but only with certain file names. Some names gives the same error again, but it seems that if name is totally new and not similar to any of the existing ones - it works. Magic?

like image 449
Denis Kim Avatar asked Dec 19 '15 22:12

Denis Kim


1 Answers

I've encountered similar issue, it seems like the complier is trying to process the file where you extend the nested class before the one where it's defined. Therefore you have this error saying that that Space has no member SomeClass.

The solution I've found is to go to your target settings, open Build Phases.

enter image description here

There, in Compile Sources section you should put the file where you define the nested class above files where you extend it.


This solution seems to even play well with your observation that when you recreate the file it sometimes compiles, because when you recreate the file its position in Compile Sources changes.

like image 175
Nikita Kukushkin Avatar answered Sep 17 '22 21:09

Nikita Kukushkin