-4

I want to carry the variables CorrectAnswerTotal and QuestionsAskedTotal across to my second view controller (Score View Controller) however I keep getting the error message 'Type 'ScoreViewController' has no member 'data''

Could someone tell me how to resolve this please.

Another issue is I am unable to call on these variables separately in my original view controller since adding them into a structure. I am a beginner at programming so is there a certain way that this should be done?

Thanks in advance.

 struct CarryToNextViewController {

var CorrectAnswerTotal = 0
var QuestionsAskedTotal = 0


}

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

let ViewController:ScoreViewController = segue.destinationViewController as! ScoreViewController
ScoreViewController.data = CarryToNextViewController(CorrectAnswerTotal: Int, QuestionsAskedTotal: Int)

}

EDIT

Even when renaming ScoreViewController as viewController I still get the same error message. This is the edited code:

  class viewController: UIViewController {


@IBOutlet var QuestionLabel: UILabel!
@IBOutlet var Buttons: [UIButton]!

var Questions = [Question]()
var QuestionNumber = Int()
var AnswerNumber = Int()

struct CarryToNextViewController {

var CorrectAnswerTotal = 0
var QuestionsAskedTotal = 0


}

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

  let ViewController:ScoreViewController = segue.destinationViewController as!  ScoreViewController
  viewController.data = CarryToNextViewController(CorrectAnswerTotal: 0, QuestionsAskedTotal: 0)

}

  • To avoid those errors – you are supposed to refer to `ViewController` rather than `ScoreViewController` conform to the naming convention by naming variable names starting with a lowercase letter and class names starting with a capital letter. – vadian Sep 13 '16 at 19:49
  • You are assigning values using class type instead of reference on type. Try ViewController.data = CarryToNextViewController(CorrectAnswerTotal: 0, QuestionsAskedTotal: 0) – Amit89 Sep 13 '16 at 19:50

2 Answers2

2

ScoreViewController is a class, not a variable. Try ViewController.data = ... instead, though you should rename it to viewController to reduce confusion.

(In fact, using standard variable/class naming throughout would make the code easier to analyze.)

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Thanks for your help, however when I change `ScoreViewController` to `viewController` I get the error message 'Use of unresolved identifier 'viewController'. – EllaHiggins Sep 13 '16 at 19:57
  • Did you rename `ViewController` where you assigned a value to it? – Phillip Mills Sep 13 '16 at 19:59
  • I've done that and the code still doesn't seem to work, I've edited it within my question so it will hopefully be clear where I've gone wrong. – EllaHiggins Sep 13 '16 at 20:10
  • @SausageMachine he didn't rename the class, his first view controller is named `viewController` – Swifty Sep 13 '16 at 20:20
  • you should check this one http://stackoverflow.com/questions/28996730/use-of-unresolved-identifier-in-swift – Nazmul Hasan Sep 13 '16 at 20:23
  • @EllaHiggins Even in the edited version you have `ViewController` and `viewController`. The compiler is case-sensitive, so it doesn't think those are the same things. – Phillip Mills Sep 14 '16 at 00:07
1

You can't use viewController.data to access let ViewController:ScoreViewController because they have different variable names(first one is UpperCase second one is LowerCase) they should be the same.

Since your first view controller is called viewController to avoid even more confusion I suggest you replace these two line:

let ViewController:ScoreViewController = segue.destinationViewController as!  ScoreViewController
viewController.data = CarryToNextViewController(CorrectAnswerTotal: 0, QuestionsAskedTotal: 0)

with

let destViewController:ScoreViewController = segue.destinationViewController as!  ScoreViewController
destViewController.data = CarryToNextViewController(CorrectAnswerTotal: 0, QuestionsAskedTotal: 0)
Swifty
  • 3,730
  • 1
  • 18
  • 23