2014-12-07 3 views
1

저는 아주 초보 프로그래머로, 사용자가 설정 한 매개 변수에 따라 임의의 식사를 제공하는 Xcode 앱을 만들려고합니다. 나는 처음부터 올바르게 설정했는지 모르겠다. 현재는 적용 가능한지를 확인한 후 매개 변수 세트를 기반으로 한 식사 이름으로 라벨을 변경하는 기능을 수행하지만 버튼 "selectRandomMealSelection"을 클릭하면 EXC_BAD_ACCESS (코드 = 2) 오류가 발생합니다.내 EXC_BAD_ACCESS (코드 = 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

당신이 * 디버그 * 문제하려고 노력합니다 (에 SEGUE 또는 presetViewController 준비)이 뷰 컨트롤러를 제시 어디든지 그때부터 setupData(....)를 호출? selectRandomMealSelection 함수에 중단 점을 설정하고 코드를 단계별로 실행하여 문제를 격리 시키십시오. –

+0

죄송합니다. 문제가있는 위치를 추가하지 않았습니다. applicabilityTest 함수의 첫 번째 줄에서 시작됩니다. @MartinR –

답변

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 
} 

그리고 당신은

+0

죄송합니다, 혼란 스럽습니다. viewDidLoad에 이니셜 라이저를 넣고 컴파일러 오류를 반환합니다 : "이니셜 라이저는 유형 내에서만 선언 될 수 있습니다"@StephenFurlani –

+0

'init() '을 전혀 호출하지 마십시오. –

-1

EXC_BAD_ACCESS (코드 = 2)가 메모리로부터 해제 된 후에 객체를 호출 할 때 발생한다. 호출하기 전에 개체의 메모리를 해제하지 않았는지 확인하십시오.

"attr0"이 (가) 문제를 일으킬 수 있다고 생각합니다.

viewdidload에서 다른 모든 것보다 먼저 super.init()를 작성하십시오. 그것은 작동해야합니다.

+1

'viewDidLoad'에서'super.init()'를 호출하면 다른 많은 문제가 발생할 수 있습니다. –

+0

오, 죄송합니다 ... 저는 init 함수에서 super.init()를 호출한다고 말하고자합니다. 죄송합니다. 활자체 오류 –

+0

그 문제가 해결되지 않았습니다. @RajatSharma –

관련 문제