3

I've the following base enum:

enum Voicity {}

On various different files, I extend the above enum to store various functions and information that is used by my app as a central station.

For instance, I use this enum to create my UI elements programmatically.

extension Voicity {
    enum sectionObject {
        case textField(ofType: Voicity.UIElement.textField)
        case button(ofType: Voicity.UIElement.button)
        case label(ofType: Voicity.UIElement.label)
        case line()
        case photoCircle
        case stackView(_: UIStackView)
        case view(_: UIView)

        var item: UIView {
            switch self {
            case .textField(let type):
                return createTextField(ofType: type)
            case .label(let type):
                return createLabel(ofType: type)
            case .line():
                return createLine()
            case .photoCircle:
                return createPhotoCircle()
            case .stackView(let view):
                return view
            case .view(let view):
                return view
            case .button(let type):
                return createButton(ofType: type)
            }
        }
    }
}

For readability/maintainability, I separated the returned functions above into separate files as well by again extending the sectionObject enum.

extension Voicity.sectionObject {
    func createLabel(ofType type: Voicity.UIElement.label) -> UILabel {
      // implementation here
    }
}

The above works fine but when I try to declare the createButton():

extension Voicity.sectionObject {
    func createButton(ofType type: Voicity.UIElement.button) -> UIButton {
      // implementation here
    }
}

I get the following error:

sectionObject is not a member of Voicity.

Why can't I extend my enum on one file when I can do so in another?

Update

createButton() and createLabel() methods are both inside group/folder Model.

The enum declarations are inside group/folder Store.

So, they are separated. But it amazes me that createLabel() works when createButton() can't.

It all used to work(methods being in different files) when my enum cases were all in a single file again under group/folder Store and methods under group/folder Model. I needed to refactor my enum cases when they enlarged.

Moreover, I just now tried moving createButton() inside the extension declaring createLabel(). And it all works for some reason.

Previously, I deleted the file declaring the createButton() method and recreated it and everything is the same. Therefore it's not some weird Xcode parsing issue.

Can
  • 4,516
  • 6
  • 28
  • 50
  • 2
    Perhaps it's a matter of access modifier. It should default to internal though, so it should be accessible package wide o.0 – Alexander Jan 05 '17 at 15:04
  • 2
    Related to Alexander's idea: are those two extensions in the same file? If not are they for sure in the same module? – HAS Jan 05 '17 at 15:23
  • @Alexander Please see the update... – Can Jan 05 '17 at 19:29
  • @HAS Please see the update... – Can Jan 05 '17 at 19:29
  • I just created the same file(CreateButton.swift) at the exact same location and everything seems to be working fine. Totally weird since previously, creating a new file didn't solve anything. – Can Jan 15 '17 at 18:22

1 Answers1

1

I found the solution here(nkukushkin's answer).

The Problem

I had the idea that it was a compilation order issue but thought that Xcode would be automatically handling it by itself. Therefore I didn't even bother to go to my project settings. Turns out, it might not always do so.

How to Solve

  1. Select your project,
  2. Select your target,
  3. Go to your target's Build Phases,
  4. Extend Compile Sources.

Xcode Project Settings | Target Build Phases | Compile Sources

Here, you're seeing a list of the files being compiled in your project.

In my case, the file I initially defined my enum sectionObject was listed under the files which I extended that enum. Therefore I had to move the definition file to the top so that the extensions can see it.

Unfortunately, Xcode(8.2.1 (8C1002)) does not allow drag-n-drop. I had to remove the file and re-add it by the plus/minus signs beneath the Compile Sources list. Once you re-add it, it gets listed on the top.

Community
  • 1
  • 1
Can
  • 4,516
  • 6
  • 28
  • 50
  • Was reorganizing a project and suddenly it could no longer build because an enum couldn't be found, even though the enum's definition file existed and was being built in this target. Never imagined build order mattered in Xcode/ Swift, thanks so much for posting this! – SafeFastExpressive Apr 13 '18 at 20:20