简体   繁体   中英

Swift 'use of undeclared type'

I'm currently trying to write a little iOS application in swift, where I have these Classes: masterTableViewController addViewController and deleteViewController , each of them is connected to a, like the name already tells, viewController. The masterTableViewController should sent some data using the predefined function:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

  if(segue.identifier == "showDetails") {
    var selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!

    var deleteViewController:deleteViewController = segue!.destinationViewController as deleteViewController

    deleteViewController.todoData = todoItems.objectAtIndex(selectedIndexPath.row) as NSDictionary
}

I want to send the data of the current row to the next, by segue referenced controller.

This is where I get an error message stating that deleteViewController is not a type that can be assigned to a variable.

But I don't really understand what the problem s right now. Basically this should work because I just want to create a new object of the type my class is of and pass this one to my view controller.

In the reference I got this code from everything worked just fine.

You are mixing class names and instance variable names. Class names should be upper case: MasterTableViewController, AddViewController, DeleteViewController

At first, try to distinguish between the class name and the instance variable name by choosing a different name, ie:

var dvc:deleteViewController = segue!.destinationViewController as deleteViewController
dvc.todoData = ...

And see if it works

Another scenario where you might get mysterious 'use of undeclared type': the type (here DeleteViewController class) is defined in a file that you renamed by changing the 'Name' text box of the 'Utilities' pane. Then Xcode MIGHT not think the file is a member of the target, even though the checkboxes in the 'Target Membership' section of the 'Utilities' pane shows that the file IS a member of the project target. The workaround is to uncheck and recheck the checkbox that makes the file a member of the target. Xcode will reindex the project files and hopefully now consider the file a member of the target.

I realize you already have an accepted answer, but I've been chasing this error for too long and finally found the cause of my issue so maybe it'll help someone keep their sanity.

I encountered this issue when trying to integrate the Google admob sdk.

Here are the steps I took to debug:

  1. Added 'import GoogleMobileAds' to the top of the view controller.
  2. Added the frameworks using CocoaPods, tried again added it manually.
  3. Checked the header search paths in Build Settings as well as the framework search paths. Verified that paths actually pointed to the frameworks I wanted to add.
  4. Checked that the framework was included in the target I was trying to build.
  5. Cleaned and tried to run, restarted and tried to run.
  6. Revisited Build Settings, noticed framework search paths had a path with hyphens "-" (my_base_dir/my_project_dir/Pods/ Google-Mobile-Ads-SDK /Frameworks). I've had issues with hyphens before so I changed the hyphens to underscores "_" and suddenly Xcode resolved the 'undeclared type' errors.

So apparently Xcode doesn't like hyphens in framework search paths.

This can occur if the class you are compiling is included in a unit test module, but the class that is not defined is not.

You will find that the app compiles and runs, but compilation errors may be shown in the source.

Open the right bar and check the Target Membership settings. They should be consistent for both classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM