Swift 如何将NSData转换为NSDictionary?
我已经尝试了我在这里找到的这个解决方案,但它不起作用,我不断地得到这个错误:Swift 如何将NSData转换为NSDictionary?,swift,core-data,nsdictionary,Swift,Core Data,Nsdictionary,我已经尝试了我在这里找到的这个解决方案,但它不起作用,我不断地得到这个错误: func showLabel() { let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate) let context :NSManagedObjectContext = appDel.managedObjectContext let noteDict = NSFetchRequest(entityNam
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
(***由于未捕获异常而终止应用程序
“NSInvalidArgumentException”,原因:'-[NSManagedObject
initWithCoder::发送到实例的选择器无法识别
0x7fb7286f36c0')
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
在将NSDictionary转换为NSData后,我将其保存到核心数据,然后我的获取看起来如下所示
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
var tempData:NSData
tempData = NSKeyedArchiver.archivedDataWithRootObject(dicResults)
let tempString = NSString(data: tempData, encoding: NSUTF8StringEncoding)
let unArchDictionary : NSDictionary? = NSKeyedUnarchiver.unarchiveObjectWithData(tempData)! as? NSDictionary
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
它正在碰撞让字典。。。
请帮忙
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
JZ考虑您自己的代码:
let dicResults : NSArray // dicResults is an array
// ...
var tempData:NSData // tempData is an NSData
tempData = NSKeyedArchiver.archivedDataWithRootObject(dicResults)
// so tempData is an array archived into an NSData
// ...
let unArchDictionary : NSDictionary? =
NSKeyedUnarchiver.unarchiveObjectWithData(tempData)! as? NSDictionary
// now you are unarchiving tempData as a dictionary?!
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
您希望如何将NSArray放入归档文件,然后从同一归档文件中获取NSDictionary?好的,当我确认有人帮助过我时,我会重新检查记录。但我解决了自己的问题
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
如果您想将NSMutableDictionary保存到核心数据并检索它,这里是解决方案仅供参考,我正在使用可转换属性..保存到核心数据..../设置ManagedObjectContext
let appDel : AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
let dict = NSEntityDescription.insertNewObjectForEntityForName("NoteDictionary", inManagedObjectContext: context)
let tempNote = self.noteField!.text as NSString
noteDictionary.setObject(tempNote, forKey: dicKey)
[dict .setValue(noteDictionary, forKey: "dictionary")]
// print("The Note Dictionary has this in it ",noteDictionary)
do {
try context.save()
print("The Note That Was Saved is ",dict)
} catch {
print(error)
}
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
现在从核心数据中检索
func showLabel() {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context :NSManagedObjectContext = appDel.managedObjectContext
let noteDict = NSFetchRequest(entityName: "NoteDictionary")
noteDict.returnsObjectsAsFaults = false
let dicResults : NSArray
try! dicResults = context.executeFetchRequest(noteDict)
for context in dicResults {
noteDictionary = context.valueForKey("dictionary")as! NSMutableDictionary
noteField?.text = noteDictionary.valueForKey(dicKey as String)as? String
}
}
嗨,我以为这条线是在解开这个物体?拿出了那本书!仍然崩溃。让我们看看字典:NSDictionary?=NSKeyedUnarchiver.unarchiveObjectWithData(tempData)为?你没按我说的做!您取出了感叹号,但没有按照我告诉您的那样取出:NSDictionary?
。这就是为什么你还在崩溃。看到我下面的答案。谢谢Matt,我确实看到了,并且解决了这个问题,还发布了一个不同的解决方案。你问了18个问题,但只有一次你接受了答案。这在堆栈溢出上是不好的行为。如果你不接受他们的答案,他们就不会愿意帮助你。如果你没有得到一个可接受的答案,你应该修改你的问题,使之更好。如果你得到了一个可接受的答案,你应该接受它(勾选)。+1另外,正在为类似问题寻找解决方案的人会看到答案不可接受的Q。