2

I thought the following would populate my home variable with my HomeViewController

var home = self.parentViewController!.parentViewController! as HomeViewController;

Instead I get the following compile error 'Use of undeclared type HomeViewController' although it definitely exists and appears in the auto complete popup.

Ben_hawk
  • 2,476
  • 7
  • 34
  • 59

3 Answers3

0

Often XCode6 displays the wrong error message. Is this an instance variable? It may be that parentViewController isn't set at init time (the fact that it's an implicitly unwrapped optional strongly suggests this). If this is in a function, I'd do this in an if let statement to give us a better sense of what's going on. Something like:

if let homeVC = self.parentViewController?.parentViewController as? HomeViewController {
    self.home = homeVC
}

This would give us at least a better opportunity to debug. Two !s in a row maybe means you're not being totally respectful of what those declarations are trying to tell you.

cmyr
  • 2,235
  • 21
  • 30
  • Hi, no its not an instance variable, just a local variable inside a button action. I tried your example but I still get the same error. What I am trying to achieve is, I have a settings view controller which is displayed as a modal over the top of the home view controller. When one of the settings option is clicked e.g 'About' I want to push the about view controller onto the home view controllers navigation stack. Actually I do not think parentViewController will work in this case as The HomeViewController is not a parent of SettingsViewController – Ben_hawk Nov 24 '14 at 19:31
  • okay, so that's the reason it's an optional. I.e. that it might not exist. This is an example where the fact that you have to use '!' is telling you a lot about what the problem might be. – cmyr Nov 24 '14 at 20:08
  • still doesn't explain the complier error, I would have expected that would give me a run time error is the optional did not exist. There seems to be a problem with the syntax – Ben_hawk Nov 24 '14 at 20:55
  • Ah I found the issue, there seems to be a bug in xcode 6, as I have this http://stackoverflow.com/questions/24924966/xcode-6-strange-bug-unknown-class-in-interface-builder-file which is why HomeViewController is not being found – Ben_hawk Nov 24 '14 at 22:05
0

The cause of this error was actually coming from a bug in xcode 6 rather than any kind of syntax error. It was related to this: Xcode 6 Strange Bug: Unknown class in Interface Builder file

I was able to fix this by clearing the projects derived data and build and restarting my machine.

Community
  • 1
  • 1
Ben_hawk
  • 2,476
  • 7
  • 34
  • 59
0

I had faced the same problem while doing an unwind segue and trying to downcast the sourceviewcontroller. I bang my head for more than 30mins and then I realized it was fairly simple and bit crazy, I had all my files except this viewcontroller added to the Tests target and after adding this viewcontroller to the Tests target, everything worked Tada!! I m saved.

anoop4real
  • 7,598
  • 4
  • 53
  • 56