Scott Scott - 2 months ago 21
iOS Question

Xcode Swift - This class is not key value coding-compliant for the key uri

I'm trying to add data to Core Data, which I think i'm on the right track (I'm fairly new to this so, apologies) From the code below, my 'Aim' is to check the Core Data and if it's empty add the default data.

Code:

class func musicIsEmpty(managedObjectContext: NSManagedObjectContext)-> Bool{

let defaultMusic = NSEntityDescription.entityForName("Music", inManagedObjectContext: managedObjectContext)
let defaultMusicSetOne = NSManagedObject(entity: defaultMusic!, insertIntoManagedObjectContext: managedObjectContext) as? Music
//defaultMusicSetOne = NSManagedObject(entity: defaultMusic!, insertIntoManagedObjectContext: managedObjectContext) as? Music
let fetchRequest = NSFetchRequest(entityName: "Music")
fetchRequest.entity = defaultMusic

defaultMusicSetOne!.uri = "spotify:track:1puJlKuYGH58SAFgXREUpE"
defaultMusicSetOne!.duration = 30.0
defaultMusicSetOne!.starttime = 49.0
defaultMusicSetOne!.xfade = 10.0
defaultMusicSetOne!.voiceover = "1.25s Next Up.mp3"

do{
if let MusicSetResults = try managedObjectContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]{

if MusicSetResults == 0
{
defaultMusic!.setValue(defaultMusicSetOne?.uri, forKey: "uri")
defaultMusic!.setValue(defaultMusicSetOne?.duration, forKey: "duration")
defaultMusic!.setValue(defaultMusicSetOne?.starttime, forKey: "starttime")
defaultMusic!.setValue(defaultMusicSetOne?.xfade, forKey: "xfade")
defaultMusic!.setValue(defaultMusicSetOne?.voiceover, forKey: "voiceover")
return true
}

else
{
return false
}
}
}
catch let error as NSError{
print("Error: \(error.debugDescription)")
}
return false
}


Music.Swift:

extension Music {

@NSManaged var voiceover: String?
@NSManaged var xfade: NSNumber?
@NSManaged var duration: NSNumber?
@NSManaged var starttime: NSNumber?
@NSManaged var uri: String?

}


But I keep getting the error "This class is not key value coding-compliant for the key uri" i'm sure this will be for all .setValues. I'm not sure why this is showing as defaultMusic does have the "uri" field and defaultMusic is also an instance of Music Object. Is the format of .setValue wrong? or is it something else?

Any help would be great!

Thanks in advance!!

Answer

I suspect you actually mean this:

Check if the entity is empty, if yes insert a new object with default values

class func musicIsEmpty(managedObjectContext: NSManagedObjectContext)-> Bool {

  let musicEntity = NSEntityDescription.entityForName("Music", inManagedObjectContext: managedObjectContext)!      
  let fetchRequest = NSFetchRequest()
  fetchRequest.entity = musicEntity
  do {
    let musicSetResults =  try managedObjectContext.executeFetchRequest(fetchRequest) as! [Music]
    if musicSetResults.isEmpty
    {
      let defaultMusicSetOne = NSManagedObject(entity: musicEntity, insertIntoManagedObjectContext: managedObjectContext) as! Music
      defaultMusicSetOne.uri = "spotify:track:1puJlKuYGH58SAFgXREUpE"
      defaultMusicSetOne.duration = 30.0
      defaultMusicSetOne.starttime = 49.0
      defaultMusicSetOne.xfade = 10.0
      defaultMusicSetOne.voiceover = "1.25s Next Up.mp3"
      return true
    }
  }
  catch let error as NSError{
    print("Error: \(error.debugDescription)")
  }
  return false
}

You might consider also the save the managed object context to make the new object persistent.

Comments