Reputation: 71
I was wondering how I could call upon previously stated variables inside of a let. I'm using Xcode Version 8.3.2 (8E2002).
I have created a TableView which shows my players (bees) and current levels. I would like to be able to upgrade a specific player, without it affecting the other players.
Here is my current coding for the table view:
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var beelevel = 0
var wasplevel = 0
var bumblebeelevel = 0
let bees = ["bee", "wasp", "bumblebee"]
let beeslevel = [beelevel, wasplevel, bumblebeelevel]
When I try using variables in the let command, it returns "Cannot use instance member 'bee level' within property initializer; property initializers before 'self' is available"
Upvotes: 0
Views: 56
Reputation: 42143
You can use lazy initialization to achieve this:
lazy var beeslevel:[Int] = [self.beelevel, self.wasplevel, self.bumblebeelevel]
Upvotes: 0
Reputation: 19892
Variables declared like that are not initialized in any specific order, so there's no way to be sure that beeLevel
will be available when initializing beesLevels
.
What you can do is override the initializers, there should be two for a UIViewController
:
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
}
required init?(coder aDecoder: NSCoder) {
}
Initialize your beesLevels
constant in there and then call super.
PS. Just a heads up, even if you change the "levels" variables, you'll always get an array of 0s on your beesLevels
array, because it's the value that's being stored, not a pointer to the variable.
If you want that array to always be up to date with those values you can declare it like this:
var beesLevels: [Int] { return [beelevel, wasplevel, bumblebeelevel] }
Upvotes: 1
Reputation: 9923
You cant use instance member while declaring coz self
have not fully init yet:
let beeslevel = [beelevel, wasplevel, bumblebeelevel]
But you can use computed property:
var beeslevel: [Int] {
return [beelevel, wasplevel, bumblebeelevel]
}
Upvotes: 1