1

I'm writing an application that takes data from a QR code scanner. I am just implementing the basics, and have a second view controller that is triggered when a new QR code is detected. The following code is in a custom view controller from RSBarcodes and implemented using CocoaPods.
It won't compile with the error

"Use of undeclared type SecondViewController".

I tried making my SecondViewController public, restarting XCode, and nothing has worked. Any help is appreciated. Thank you!

override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destinationVC = segue.destinationViewController as! SecondViewController
    destinationVC.label = "test"  

}

SOLUTION:

Added to "Copy Bundle Resources" and then my VC was immediately recognized by the compiler.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
rb612
  • 5,280
  • 3
  • 30
  • 68
  • Can you also post the code containing he declaration of `SecondViewController`? – Romain May 04 '15 at 06:09
  • Did you gone through this post, http://stackoverflow.com/questions/25305945/use-of-undeclared-type-viewcontroller-when-unit-testing-my-own-viewcontroller. , I mean to say have you added your View Controller swift file in the Target membership? – Vizllx May 04 '15 at 06:10
  • It's a default Cocoa Touch class that extends UIViewController. It has no method overrides, just an IBOutlet for the label. I'm really confused about targets and similar stuff, so that might be an issue. – rb612 May 04 '15 at 06:12
  • How is the class declared? Your file may be named `SecondViewController` but still doesn't mean anything if your class is: `class Secon_ViewController: UIViewController { }` – Eendje May 04 '15 at 06:12
  • It's declared as `class SecondViewController: UIViewController { }`, and Vizllx yes I did and nothing changed. – rb612 May 04 '15 at 06:15
  • When you type it in `prepareForSegue` does it autocomplete? – Eendje May 04 '15 at 06:19
  • Yes it does. Here is a view of my file structure. The error is in RSCodeReaderViewController. Is it something with how my files are structured? https://www.dropbox.com/s/xwva7323qo6shps/Screenshot%202015-05-04%2001.18.44.png?dl=0 – rb612 May 04 '15 at 06:20
  • Have you added the file in Test target too? just check once is it got added to the main target and not the test target. – Vizllx May 04 '15 at 06:36
  • 1
    Also see whether the file is added in Copy Bundle Resources path along with storyboard and images. – Vizllx May 04 '15 at 06:39

2 Answers2

0

SOLUTION:

Added to "Copy Bundle Resources" and then my VC was immediately recognized by the compiler.

rb612
  • 5,280
  • 3
  • 30
  • 68
-2

First you have to verify which segue you're pushing to by checking the segue identifier name. Then you have to check if you can even create the destinationVC variable, and then if you can unwrap it using '?' instead of '!'.

override public func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?) {
    if segue.identifier == "nameOfMySegue" {
        if let destinationVC = segue.destinationViewController as? SecondViewController {
            destinationVC.label = "test"
        }
    }
}
user3353890
  • 1,844
  • 1
  • 16
  • 31
  • I appreciate your response. However, it's not an issue about unwrapping optional types. We're doing an explicit downcast and it doesn't recognize the SecondViewController at all. – rb612 May 04 '15 at 06:22
  • The only reason I say it might be an optional issue is because Apple made some language changes to Swift in regards to optional unwrapping with the most recent version of Xcode. I myself had some issues with explicitly downcasting variables. It might not be your issue, but I feel like it's a simple enough code change to warrant trying. – user3353890 May 04 '15 at 06:29
  • Good point. I just tried and it still gives me the same error. – rb612 May 04 '15 at 06:32