数组索引超出范围-可选取消绑定时出错 [英] Array Index Out Of Range - Error when optional unbinding

查看:82
本文介绍了数组索引超出范围-可选取消绑定时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Settings的实体,该实体的类型为Int的名为backgroundColor的属性,如果为 1 ,则如果 0,则视图控制器将具有白色背景,然后是深灰色的背景.

I have an entity called Settings with an attribute called backgroundColor of type Int, if it is 1 then the view controller will have a background of white if 0 then a background of dark grey.

但是尝试打开视图控制器时出现以下错误;

But I am getting the following error when trying to open the view controller;

严重错误:数组索引超出范围

fatal error: Array Index out of range

对于函数的下一行

    if settingsArray.count == 1 {
        setting = settingsArray[1]
    } else if settingsArray.count <= 0 {
      println("No settings in array")
    }

视图控制器

var settingsArray: [Settings]! 
var setting: Settings!
var backgroundSetting: Bool = true

override func viewWillAppear(animated: Bool) {
    backgroundSettings()
}

override func viewDidLoad() {
    super.viewDidLoad()
    backgroundSettings()
}

// Function to fetch settings and change background
func backgroundSettings() {

    var error: NSError?
    let request = NSFetchRequest(entityName: "Settings")
    self.settingsArray = moc?.executeFetchRequest(request, error: &error) as! [Settings]

    if settingsArray.count == 1 {
        setting = settingsArray[1]
    } else if settingsArray.count <= 0 {
      println("No settings in array")
    }

    if setting != nil {

        if setting.backgroundColor == 1 {
            backgroundSetting = true
        } else if setting.backgroundColor == 0{
            backgroundSetting = false
        }

    }


    if backgroundSetting == true {
        self.view.backgroundColor = UIColor.whiteColor()
    } else if backgroundSetting == false {
        self.view.backgroundColor = UIColor.darkGrayColor()
    }

}

//Button to change the color and settings
@IBAction func backgroundColor(sender: AnyObject) {


    if setting != nil {

        if setting.backgroundColor == 1 {
            setting.backgroundColor = 0
        } else {
            setting.backgroundColor = 1
        }

        var error: NSError?
        moc?.save(&error)

    } else {

        println("No settings available")

        var settings = NSEntityDescription.insertNewObjectForEntityForName("Settings", inManagedObjectContext: moc!) as! Settings

        settings.backgroundColor = 1

        var error: NSError?
        moc?.save(&error)

    }

    backgroundSettings()

}

有什么想法我可能会出错吗?

Any ideas where I may be going wrong ?

推荐答案

ETA:

我一直在考虑这个问题.我将旧答案留在下面,以便前面的评论有意义.

I have been having another think about this. I have left my old answer below so that the previous comments make sense.

if let thisSetting = settingsArray?[0]中...如果settingsArraynil,则右侧可能有效地是nil[0].因此,我相信这可以消除崩溃:

In if let thisSetting = settingsArray?[0] … if settingsArray is nil then the right side is potentially effectively nil[0]. Therefore I believe that this may eliminate the crash:

// ...
if !settingsArray.isEmpty {
    if let thisSetting = settingsArray?[0] {
        setting = thisSetting
    }
}

我认为

settingsArray[0]nil会是一个单独的问题.我认为这与生命周期有关.

settingsArray[0] being nil would I think then be a separate issue. Which I think would relate to the lifecycle.

先前的回答如下:

我认为问题可能是由您从viewDidLoad()调用func backgroundSettings()引起的-即在View Controller生命周期的早期-在值初始化之前.

I believe that the problem may be being caused by you calling func backgroundSettings() from viewDidLoad() - i.e. too early in the View Controller lifecycle - and before the values have been initialized.

这篇关于数组索引超出范围-可选取消绑定时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆