我是一個非常新手的程序員,我正嘗試創建一個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}
}
你有沒有試過*調試*問題?在selectRandomMealSelection函數中設置一個斷點,單步執行代碼等等來隔離你的問題? – 2014-12-07 10:31:23
對不起,我沒有添加問題所在,它從applicabilityTest函數的第一行開始。 @MartinR – 2014-12-07 10:50:27