branimal branimal - 1 year ago 334
Swift Question

Swift NSManagedObject does not conform to protocol sequence Type

I have an NSManagedObject called Media which stores a picture and a description.

Here's the class and extension definition:

class Media: NSManagedObject{}

extension Media {

@NSManaged var desc: String?
@NSManaged var image: NSData?
@NSManaged var carRelationship: Car?


Swift stores the image without issue:

myMedia = NSEntityDescription.insertNewObjectForEntityForName("Media", inManagedObjectContext: managedObjectContext) as! Media
myMedia.image = NSData(data: UIImageJPEGRepresentation(image, 1)!)
car.mediaRelationship = myMedia

But when I go to retrieve it, the compiler complains: (doesnt compile)

for pic in car.mediaRelationship {


The error is:

Type 'Media?' does not conform to protocol 'Sequence Type'

Answer Source

Check if you have @NSManaged var mediaRelationship: NSSet? in your car NSManagedObject. Don't forget to unwrap (if let) mediaRelationship when retrieve set. Also check if inverse relationship is set