Uitableview Swift-致命错误:Can';t展开可选。无
我正在尝试获取核心数据对象并在UITableView中显示它们 我有一个Uitableview Swift-致命错误:Can';t展开可选。无,uitableview,core-data,swift,ios8,Uitableview,Core Data,Swift,Ios8,我正在尝试获取核心数据对象并在UITableView中显示它们 我有一个DBEmployess对象,当我试图获取它的name属性时,我得到一个错误 fatal error: Can't unwrap Optional.None 这是我的密码 dbemployeess.h #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @class DBProjects; @interface DBEmp
DBEmployess
对象,当我试图获取它的name
属性时,我得到一个错误
fatal error: Can't unwrap Optional.None
这是我的密码
dbemployeess.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class DBProjects;
@interface DBEmployess : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * designation;
@property (nonatomic, retain) NSNumber * age;
@property (nonatomic, retain) DBProjects *projects;
@end
#import "MBProgressHUD.h"
#import "DBEmployess.h"
MyProject桥接头.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class DBProjects;
@interface DBEmployess : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * designation;
@property (nonatomic, retain) NSNumber * age;
@property (nonatomic, retain) DBProjects *projects;
@end
#import "MBProgressHUD.h"
#import "DBEmployess.h"
UITableView方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
if(cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
let employee : DBEmployess? = self.employessArray.objectAtIndex(indexPath.row) as? DBEmployess
if(employee != nil) {
cell!.textLabel.text = employee!.valueForKey("name").description
}
return cell!
}
如果我打印employee
对象,我会得到
(DBEmployess?) employee = Some {
Some = 0x0000000111702670 {
CoreData.NSManagedObject = {
ObjectiveC.NSObject = {}
}
}
知道我做错了什么吗
更新:
我发现实际的问题是在保存核心数据时。如果我在NSManagedObject
的属性中设置硬编码字符串,那么它完全可以访问,并且显示在UITableView中。但是如果我设置了UITextField
text的值,它确实成功保存了,但无法展开
这是我的核心数据
func addNewEmployee() {
let delegate = UIApplication.sharedApplication().delegate as AppDelegate
let context = delegate.managedObjectContext
let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName("DBEmployess", inManagedObjectContext: context) as NSManagedObject
newManagedObject.setValue(self.txtName.text, forKey: "name")
newManagedObject.setValue(self.txtDesignation.text, forKey: "designation")
newManagedObject.setValue(self.txtAge.text, forKey: "age")
// Save the context.
var error: NSError? = nil
if !context.save(&error) {
var alert = UIAlertController(title: "Error", message: "Could not save employee \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Saved", message: "Employee saved successfully", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
如果可选对象为
nil
,则无法将其展开;否则你会像以前一样崩溃。对于单元格
和员工
都是如此。哪一行抛出异常?我有println()DBEmployess,它似乎不是null。您可以在我的问题中看到log。那么,让我再试一次,哪一行抛出了execption?我无法访问name
property如果name
isnil
,这将出错,因为valueForKey
返回可选值。我认为如果让name=employee?.valueForKey(“name”){cell!.textLabel.text=name}或者{//NO name!}