저는 아주 초보 프로그래머로, 사용자가 설정 한 매개 변수에 따라 임의의 식사를 제공하는 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}
}
당신이 * 디버그 * 문제하려고 노력합니다 (에 SEGUE 또는
presetViewController
준비)이 뷰 컨트롤러를 제시 어디든지 그때부터setupData(....)
를 호출? selectRandomMealSelection 함수에 중단 점을 설정하고 코드를 단계별로 실행하여 문제를 격리 시키십시오. –죄송합니다. 문제가있는 위치를 추가하지 않았습니다. applicabilityTest 함수의 첫 번째 줄에서 시작됩니다. @MartinR –