Hunter
Hunter

Reputation: 1391

Instance member Goals cannot be used on type "ViewController" SWIFT XCODE

Hey I'm trying to Categorised or basically manually list stats in a way where i can give the program conditions to find exactly what I'm looking for so like if i was looking for how many goals where scored in the first 0 to 45 (first half) of a game that Barcelona was playing in on a specific day i could. Say in code find me all the games that were played on this date and where two or more goals were score in the first 0 to 45 minutes. As I'm not sure if i did it right but i tried to give the strings a number/Int value to represent the minute the goal was score in the game. And i also did the same with the date for the game. But the problem is there's an error at (don't Mind The spelling error lol "Barcelona")

 let BarcelonavsRealMadrid1 = [Goals, Penaltys]

Instance member Goals cannot be used on type "ViewController"

also if theres a better way to do this please you can rewrite my code. Thanks

Code:

import UIKit

class ViewController: UIViewController {

     let Games = [BarcelonavsRealMadrid1: 1/13/14]
     let SpainPrimeraDivision = [RealMadrid, Bareclonia]
     let RealMadrid = [BarcelonavsRealMadrid1: 1/12/14]
     let Bareclonia = [BarcelonavsRealMadrid1: 1/12/14]

     let BarcelonavsRealMadrid1 = [Goals, Penaltys]
     let Goals : [String : Int] = ["BarcelonaGoal":21,"RealMadridGoal":23]
     let Penaltys  : [String : Int] = ["RealMadridPenalty":21,"BarcelonaPenalty":23]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


} 

Upvotes: 0

Views: 651

Answers (3)

luk2302
luk2302

Reputation: 57134

You cannot reference one instance variable in the default assignment of another one. The following is the simplified version of your problem:

class ViewController: UIViewController {
    let foo = 12
    let bar = foo
}

Normally you have to setup the variables in the init method. But since you have a UIViewController here, you have to do it in viewDidLoad. You therefore have to write

class ViewController: UIViewController {

    var foo : Int!
    var bar : Int!

    override func viewDidLoad() {
        super.viewDidLoad()
        foo = 12
        bar = foo
    }
}

Your code has a few more problems than that however:

  • you are trying to access variables before they are declared all over the place
  • what is 1/12/14 supposed to be? I guarantee you it is not a date
  • stop starting variable names with upper case letters

What you have to do:

  • rename all your variables
  • change all you variables to var and assign value to them in the correct order inside viewDidLoad
  • deal with the date situation

Upvotes: 2

Bhavin Bhadani
Bhavin Bhadani

Reputation: 22374

Your issue is that you are making references between class properties during initialization - this isn't permitted. One way around this is to set some of the things initially, then in viewDidLoad or in viewWillAppear set the dependencies, like this:

//-------All Games Ever played------------------------------------
var BarcelonavsRealMadrid1 = [[String : Int]]()  // if you want this globally
let Goals : [String : Int] = ["BarcelonaGoal":21,"RealMadridGoal":23]
let Penaltys  : [String : Int] = ["RealMadridPenalty":21,"BarcelonaPenalty":23]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    BarcelonavsRealMadrid1 = [Goals, Penaltys]
}

also there were few more problems suggested by @luk2302 in his answer

Upvotes: 1

Pradeep K
Pradeep K

Reputation: 3661

Try this

var BarcelonavsRealMadrid1:[[String:Int]] { return [Goals, Penaltys] }

Upvotes: 1

Related Questions