Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSManagedObject to 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)

But I get error:

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

What am I doing wrong here?

like image 273
1110 Avatar asked Apr 16 '15 06:04

1110


1 Answers

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

Another problem is that order is apparently an optional, so it needs to 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
}
like image 51
Martin R Avatar answered Nov 05 '22 05:11

Martin R