我有一個問題,將值傳遞給swift中的類。我有ViewControler.swift創建對象和其他UI類型的東西。下面是代碼:「計數」不能用於'ViewController'類型
class ViewController: UIViewController {
//creates the Intractive button objects
@IBOutlet var Bag1: UIButton!
@IBOutlet var Bag2: UIButton!
@IBOutlet var Bag3: UIButton!
@IBOutlet var lblTokenSlider: UILabel!
@IBOutlet var slider: UIStepper!
@IBOutlet var Go: UIButton!
@IBOutlet var counter: UILabel!
var count = 10
var noOfBags = 3
//gives acces to game AP!
var gameAPI = GameAPI(noOfBags: count, noOfTokens: noOfBags)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lblTokenSlider.hidden = true
slider.hidden = true
Go.hidden = true
counter.hidden = true
Bag3.hidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Event Drivers
@IBAction func btnBag1(sender: AnyObject) {
Bag1.userInteractionEnabled = false
Bag2.userInteractionEnabled = false
Bag3.userInteractionEnabled = false
}
@IBAction func btnBag2(sender: AnyObject) {
}
@IBAction func btnBag3(sender: AnyObject) {
}
@IBAction func SliderAction(sender: AnyObject) {
}
@IBAction func RemoveTokens(sender: AnyObject) {
}
}
}
這裏是我的GameAPI:
import Foundation
private enum MoveError: ErrorType {
case Empty
}
class GameAPI {
var noOfBags: Int
var bagArray:[Bag] = []
init(noOfBags: Int, noOfTokens : Int){
self.noOfBags = noOfBags
for _ in 0..<noOfBags {
bagArray.append(Bag(counter: noOfTokens))
}
}
/* Returns the amount of counters are in a bag */
func getCounts(i :Int) -> Int {
return bagArray[i].getCount()
}
func isBagEmpty(i: Int) -> Bool {
if (bagArray[i].getCount() <= 0){
return true
}
else {
return false
}
}
func removeCounter(bagToRemove: Int, counters: Int) throws{
do {
try self.bagArray[bagToRemove].removeCount(counters)
}
catch{
throw MoveError.Empty
}
}
}
的問題是,我在ViewController中聲明的GameAPI,我得到 「實例成員‘計數’不能在類型使用「的ViewController」」 上線 var gameAPI = GameAPI(noOfBags: count, noOfTokens: noOfBags)
但是,如果我是切換變量並使用固定值,如: var gameAPI = GameAPI(noOfBags: 10, noOfTokens: 3)
它工作正常。我真的不明白爲什麼這不起作用。
謝謝
@Hamish感謝您的評論,我不知道偷懶變量的語法,因爲我不使用它們經常。 – Sulthan
沒問題:)我已經刪除了我的評論,現在你已經做出了改變。 – Hamish
謝謝!我使用了靜態,我嘗試了你的第四個建議,但是出現錯誤''Value'type'NSObject - >() - > ViewController'沒有成員'count''' – Osian