I'm having an issue with making a NSDictionary
to get the an object in my CoreData Model.
let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Notificatie), withPredicate: nil, managedObjectContext: moc)
for notificatie in results
{
let singleNotificatie:Notificatie = notificatie as Notificatie
let notDict:NSDictionary = ["title":notificatie.title, "fireDate":notificatie.fireDate]
}
Cannot convert the expression's type 'NSDictionary' to type 'StringLiteralConvertible'
The problem is in the Dictionary.
Why can't I cast a String in to get the object? Any solution?
I presume that the title
and fireDate
are optional properties - if yes, that's the reason. A NSDictionary
cannot store nil
values.
You should either:
If this is how your Notificatie
class looks like:
class Notificatie {
var title: String?
var fireDate: NSDate?
}
using the 1st option you should add to the dictionary only non-nil values:
let notDict:NSDictionary = NSDictionary()
if let title = notificatie.title {
notDict.setValue(title, forKey: "title")
}
if let fireDate = notificatie.fireDate {
notDict.setValue(fireDate, forKey: "fireDate")
}
If instead you opt for the swift dictionary:
let notDict: [String:AnyObject?] = ["title":notificatie.title, "fireDate": notificatie.fireDate]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With