2014-12-07 166 views
1

我是一個非常新手的程序員,我正嘗試創建一個Xcode應用程序,它根據用戶設置的參數爲用戶提供隨機餐。我不知道我是否已經把它放在第一位。目前它只是在檢查是否適用之後,根據設置的參數將標籤更改爲用餐名稱,但當單擊「selectRandomMealSelection」按鈕時,出現EXC_BAD_ACCESS(Code = 2)錯誤。我的EXC_BAD_ACCESS(Code = 2)錯誤的原因是什麼?

我願意接受任何批評,但請去容易對我,我只學習了一個月了互聯網,但仍不能100%肯定它是如何工作。謝謝。

下面是代碼:

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. 
} 

init(attr0: Bool, attr1: Bool, attr2: Bool, attr3: Bool, attr4: Bool, attr5: Bool, randomMealCaseNumber: Int, cuisine: [String]) 
{ 
    self.attr0 = attr0 
    self.attr1 = attr1 
    self.attr2 = attr2 
    self.attr3 = attr3 
    self.attr4 = attr4 
    self.attr5 = attr5 
    self.randomMealCaseNumber = randomMealCaseNumber 
    self.cuisine = cuisine 

    super.init() 
} 


required init(coder aDecoder: NSCoder) 
{ 
    super.init(coder: aDecoder) 
} 

var cuisine = ["Chinese", "Indian"] 

@IBOutlet var healthSwitch: UISwitch! 
@IBOutlet var easeSwitch: UISwitch! 
@IBOutlet var timeSwitch: UISwitch! 
@IBOutlet var costSwitch: UISwitch! 

var attr0 = true   //Chinese 
var attr1 = false   //Indian 
var attr2 = false   //Health 
var attr3 = false   //Ease 
var attr4 = false   //Time Sensitivity 
var attr5 = false   //Cost Sensitivity 


@IBOutlet var mealNameLabel: UILabel! 

@IBAction func healthIsRelevant(sender: UISwitch) { 
    if healthSwitch.on == true{ 
     attr2 = true 
    } 
    else if healthSwitch.on == false { 
     attr2 = false 
    } 
} 

@IBAction func easeIsRelevant(sender: UISwitch) { 
    if easeSwitch.on == true { 
     attr3 = true 
    } 
    else if easeSwitch.on == false { 
     attr3 = false 
    } 
} 

@IBAction func timeIsRelevant(sender: UISwitch) { 
    if timeSwitch.on == true { 
     attr4 = true 
    } 
    else if timeSwitch.on == false { 
     attr4 = false 
    } 
} 

@IBAction func costIsRelevent(sender: UISwitch) { 
    if costSwitch.on == true { 
     attr5 = true 
    } 
    else if costSwitch.on == false { 
     attr5 = false 
    } 
} 

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 
{ 
    return 1 
} 

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
{ 
    return cuisine.count 
} 

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String 
{ 
    return cuisine[row] 
} 

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
{ 
    var cuisineSelected = "\(cuisine[row])" 

    if cuisineSelected == cuisine[0] { 
     attr0 = true 
     attr1 = false 
    } 
    else if cuisineSelected == cuisine[1] { 
     attr0 = false 
     attr1 = true 
    } 
} 

let PorkChowMeinNoodles = (true, false, false, true, false, true) 
func porkChowMeinNoodles (name: UILabel) { 
    name.text = "Pork Chow Mein Noodles" 
} 
let ButterChicken = (false, true, false, false, false, false) 
func butterChicken (name: UILabel) { 
    name.text = "Butter Chicken" 
} 

var x = 0 

var randomMealCaseNumber: Int = Int(arc4random() % 2) 

@IBAction func selectRandomMealSelection(sender: UIButton) { 
    randomMealSelection() 


} 

func randomMealSelection() { 

    var randomMealCaseNumber: Int = Int(arc4random() % 2) 

    switch (randomMealCaseNumber) { 
    case 0: 
     applicabilityTest(ButterChicken, a: attr0 ,b: attr1 ,c: attr2 ,d: attr3,e: attr4,f: attr5) 
     if x == 1 { 
      butterChicken(mealNameLabel) 
     } 
     else if x == 0 { 
      randomMealSelection() 
     } 
    case 1: 
     applicabilityTest(PorkChowMeinNoodles, a: attr0 ,b: attr1 ,c: attr2 ,d: attr3,e: attr4,f: attr5) 
     if x == 1 { 
      porkChowMeinNoodles(mealNameLabel) 
     } 
     else if x == 0 { 
      randomMealSelection() 
     } 
    default: 
     break; 
    } 
} 

func applicabilityTest (meal: (Bool,Bool,Bool,Bool,Bool,Bool), a: Bool, b: Bool, c: Bool, d: Bool, e: Bool, f: Bool) { 
    var a = attr0 
    var b = attr1 
    var c = attr2 
    var d = attr3 
    var e = attr4 
    var f = attr5 

    if meal.0 == a { 
     if meal.1 == b { 
      if meal.2 == c { 
       if meal.3 == d { 
        if meal.4 == e { 
         if meal.5 == f { 
          x += 1 
         } 
         else 
         {x += 0} 
        } 
        else 
        {x += 0} 
       } 
       else 
       {x += 0} 
      } 
      else 
      {x += 0} 
     } 
     else 
     {x += 0} 
    } 
    else 
    {x += 0} 
} 
+0

你有沒有試過*調試*問題?在selectRandomMealSelection函數中設置一個斷點,單步執行代碼等等來隔離你的問題? – 2014-12-07 10:31:23

+0

對不起,我沒有添加問題所在,它從applicabilityTest函數的第一行開始。 @MartinR – 2014-12-07 10:50:27

回答

0

你的方法

init(attr0: Bool, attr1: Bool, attr2: Bool, attr3: Bool, attr4: Bool, attr5: Bool, randomMealCaseNumber: Int, cuisine: [String]) 

永遠不會被調用。將此邏輯放在viewDidLoad方法中。甲ViewController的對象的生命週期(init & dealloc)由UIKit的控制。一般來說,你不應該爲你的代碼邏輯使用這些方法。


[編輯]

setupData(attr0: Bool, attr1: Bool, attr2: Bool, attr3: Bool, attr4: Bool, attr5: Bool, randomMealCaseNumber: Int, cuisine: [String]) 
{ 
    self.attr0 = attr0 
    self.attr1 = attr1 
    self.attr2 = attr2 
    self.attr3 = attr3 
    self.attr4 = attr4 
    self.attr5 = attr5 
    self.randomMealCaseNumber = randomMealCaseNumber 
    self.cuisine = cuisine 
} 

然後調用從setupData(....)無論你呈現這個視圖控制器(在對SEGUE或presetViewController準備)

+0

對不起,我很困惑。我把初始化器放在viewDidLoad中,它返回一個編譯器錯誤:「初始化器只能在一個類型中聲明」@StephenFurlani – 2014-12-08 05:30:35

+0

根本不要調用'init() – 2014-12-08 18:57:01

-1

EXC_BAD_ACCESS(代碼= 2),當它被從存儲器釋放後的對象被稱爲發生。在調用它之前,確保你沒有釋放任何對象的內存。

我想你「attr0」可能會造成問題。

在您的viewDidLoad,嘗試寫一切之前super.init()。它應該工作。

+1

在'viewDidLoad'中調用'super.init()'可能會導致很多其他問題。 – 2014-12-07 17:09:00

+0

ohh我很抱歉...我打算說在你的init函數中調用super.init()。對不起,這是印刷錯誤 – 2014-12-07 18:27:27

+0

這並沒有解決它。 @RajatSharma – 2014-12-08 05:24:49

相關問題