我试图保存到我的核心数据,就像我之前做了一千次,然后突然出现这个错误
' NSInvalidArgumentException',原因:'不可接受的值类型 attribute:property =" messageType&#34 ;;所需类型= NSNumber;特定 type = DemoApp.SendMessageViewController;值= DemoApp.SendMessageViewController'
根本没有任何意义,因为属性被定义为double
,我绝对给它一个NSNumber。我的代码如下所示:
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let en = NSEntityDescription.entityForName("Message", inManagedObjectContext: contxt)
var newMessage = Message(entity: en!, insertIntoManagedObjectContext: contxt)
newMessage.receiver = receiver
newMessage.sender = PFUser.currentUser().username
newMessage.readByReceiver = false
newMessage.readBySender = false
newMessage.dateSent = NSDate()
if memberMessage{
newMessage.messType = 2
}else{
newMessage.messType = 0
}
contxt.save(nil)
对于为什么会发生这种情况的任何建议都会受到赞赏,正如我已经说过的,我之前已经使用过这段代码,而且应该可以使用。
编辑:我的消息类
@objc(Message)
class Message: NSManagedObject {
@NSManaged var dateSent: NSDate
@NSManaged var messType: Double
@NSManaged var revealingMessage: Bool
@NSManaged var receiver: String
@NSManaged var sender: String
@NSManaged var readByReceiver: Bool
@NSManaged var readBySender: Bool
}