我是一名非常新手的程序员,我正在尝试创建一个Xcode应用程序,根据用户设置的参数为用户提供随机餐。我不知道我是否一开始就把它设置好了。目前它只是在检查它是否适用但是在我点击按钮" selectRandomMealSelection&#34时已经获得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 :(得分: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
}
然后从呈现此视图控制器的任何地方(在准备segue或setupData(....)
)中调用presetViewController
答案 1 :(得分:-1)
在从内存中释放对象后调用对象时发生EXC_BAD_ACCESS(代码= 2)。在调用之前,请确保没有释放任何对象的内存。
我认为你的“attr0”可能会引发问题。
在你的viewdidload中,尝试在其他所有内容之前编写super.init()。它应该工作。