0

My project structure/dependencies are:

AppDelegate --> Imports "FirstRunViewController" and "MainViewController"

  • If the user is logged in, it sets the rootViewController to MainViewController
  • If the user is not logged in, it sets the rootViewController to FirstRunViewController

FirstRunViewController --> Imports "MainViewController"

  • After login/sign up, it sets the viewController as MainViewController

MainViewController --> Imports "SettingsViewController"

  • Presents SettingsViewController when needed

SettingsViewController --> Imports "FirstRunViewController"

  • If the user chooses to log out, it sets the viewController as FirstRunViewController

My Issue

Importing "FirstRunViewController" in "SettingsViewController" (to handle logging out) causes Xcode to not be able to find a valid delegate in another, unrelated ViewController. The specific error code is "No type or protocol named ABCDelegate".

Deleting that "#import" statement will allow the code to run and compile, but disables logging out.

My Question

Why does this happen?

How can I stop this from happening?

Ryan D'souza
  • 583
  • 9
  • 21
  • 3
    But where is ABCDelegate declared? Also please show the actual import statements and where they are: do not just say "imports" because who knows what you're really doing? Don't _describe_ your code; _show_ it. Much more reliable and clear if you do. – matt Apr 03 '16 at 03:38
  • Try using `@class` instead `import` – Martin Le Apr 03 '16 at 05:15

2 Answers2

2

I am not sure . But with your flow i can feel like This occurs because of circular dependency.

Solution :-

Use .pch file and import your all file in .pch file and use it according to your requirement.

Regarding detail discussion on .pch file check this .

what-is-prefix-pch-file-in-xcode

Community
  • 1
  • 1
Badal Shah
  • 7,255
  • 2
  • 27
  • 59
1

You likely have a circular dependency issue. I'll explain with an example.

A imports B. A requires B to already be compiled so it can refer to its methods. We say that B is a dependency of A. So Xcode automatically arranges the order in which it compiles things:

  1. B
  2. A

So that when A is compiled, B is already compiled. However, what happens when we add a third class, C. C imports A, but B imports C.

  1. B
  2. A
  3. C

But now when the compiler tries to compile B, C has not been compiled so it complains that it cannot find C.

But simply changing the order won't change things because C requires A but A requires B but B requires C. It's a circular dependency.

One way to solve this is to create a precompiled header (see the question that Badal Shah linked). This solves the problem by essentially promising that C exists and just hasn't been compiled yet, and will let B compile successfully which lets the whole loop compile successfully.

WolfLink
  • 3,200
  • 2
  • 25
  • 41