简体   繁体   English

Swift-核心数据运行时错误

[英]Swift - Core data runtime error

I'm getting this runtime error when trying to create an array of the textLabels in my cells. 尝试在单元格中创建textLabels数组时遇到此运行时错误。

My code looks like this: 我的代码如下所示:

    else {
        mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
        mySelectedCell.tintColor = UIColor.blackColor()

        if let tx = mySelectedCell.textLabel?.text as Optional?{

            var textLabel:String = String()                
            textLabel = tx!                
            var tempFriend = Model(entity: en!, insertIntoManagedObjectContext: context)

            //Save user to core data                
            tempFriend.tempUser = textLabel

            //Save context 
            context.save(nil)

            //Make list from objects   
            liste = context.executeFetchRequest(freq, error: nil)!

            //Make new list of strings from first list

            for var i = 0; i < liste.count; ++i{

                var data:NSManagedObject = liste[i] as NSManagedObject
                //The next line is where the error appears
                 showList.append(data.valueForKeyPath("tempUser") as String)
            }

            //Show list                
            println(showList)

          }
        }
      }

My error says: 我的错误说:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb). 致命错误:解开可选值(lldb)时意外发现nil。

I do not understand why something would return "nil" in my code. 我不明白为什么某些代码会返回"nil"

Any thoughts would be appreciated. 任何想法将不胜感激。

You're force-unwrapping an optional value that is nil. 您正在强制展开一个为nil的可选值。 It's hard to tell which one, since you've omitted the stack trace for the error. 很难确定是哪一个,因为您已省略了错误的堆栈跟踪。 Try this: 尝试这个:

else {
    mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
    mySelectedCell.tintColor = UIColor.blackColor()

    if let textLabel = mySelectedCell.textLabel?.text,
       let entity = en {

        var tempFriend = Model(entity: entity, insertIntoManagedObjectContext: context)

        //Save user to core data                
        tempFriend.tempUser = textLabel

        //Save context 
        context.save(nil)

        //Make list from objects   
        liste = context.executeFetchRequest(freq, error: nil)!

        //Make new list of strings from first list

        for var i = 0; i < liste.count; ++i{

            var data:NSManagedObject = liste[i] as NSManagedObject
            //The next line is where the error appears
             showList.append(data.valueForKeyPath("tempUser") as String)
        }

        //Show list                
        println(showList)

      }
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM