本文介绍了如何将NSManagedObject转换为NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 NSManagedObject 转换为 NSDictionary 这是我尝试:

I am trying to convert NSManagedObject to NSDictionary this is what I tried:

var keys:NSArray = order?.entity.attributesByName.keys
var dict:NSDictionary = order?.dictionaryWithValuesForKeys(keys)

但我收到错误:

 LazyForwardCollection<MapCollectionView<Dictionary<NSObject,
 AnyObject>, NSObject>>? is not convertible to NSArray.

我在这里做错了什么?

推荐答案

字典的属性返回 LazyForwardCollection 转换为实数数组。

The keys property of a dictionary returns a LazyForwardCollection which has to be converted to a real array.

另一个问题是, order 显然是一个可选项,因此需要
才能解开。可选绑定。

Another problem is that order is apparently an optional, so it needsto be unwrapped, e.g. with optional binding.

if let theOrder = order {
    let keys = Array(theOrder.entity.attributesByName.keys)
    let dict = theOrder.dictionaryWithValuesForKeys(keys)
} else {
    // order is nil
}

这篇关于如何将NSManagedObject转换为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:13