Swift NSManagedObject 不符合协议序列类型
Swift NSManagedObject does not conform to protocol sequence Type
我有一个名为 Media
的 NSManagedObject
,它存储图片和描述。
这是 class 和扩展定义:
class Media: NSManagedObject{}
extension Media {
@NSManaged var desc: String?
@NSManaged var image: NSData?
@NSManaged var carRelationship: Car?
}
Swift 存储图像没有问题:
myMedia = NSEntityDescription.insertNewObjectForEntityForName("Media", inManagedObjectContext: managedObjectContext) as! Media
myMedia.image = NSData(data: UIImageJPEGRepresentation(image, 1)!)
car.mediaRelationship = myMedia
但是当我去检索它时,编译器抱怨:(不编译)
for pic in car.mediaRelationship {
}
错误是:
Type 'Media?' does not conform to protocol 'Sequence Type'
检查您的汽车中是否有 @NSManaged var mediaRelationship: NSSet?
NSManagedObject。不要忘记在检索集时解包(如果允许的话)mediaRelationship。
还要检查是否设置了反向关系
我有一个名为 Media
的 NSManagedObject
,它存储图片和描述。
这是 class 和扩展定义:
class Media: NSManagedObject{}
extension Media {
@NSManaged var desc: String?
@NSManaged var image: NSData?
@NSManaged var carRelationship: Car?
}
Swift 存储图像没有问题:
myMedia = NSEntityDescription.insertNewObjectForEntityForName("Media", inManagedObjectContext: managedObjectContext) as! Media
myMedia.image = NSData(data: UIImageJPEGRepresentation(image, 1)!)
car.mediaRelationship = myMedia
但是当我去检索它时,编译器抱怨:(不编译)
for pic in car.mediaRelationship {
}
错误是:
Type 'Media?' does not conform to protocol 'Sequence Type'
检查您的汽车中是否有 @NSManaged var mediaRelationship: NSSet?
NSManagedObject。不要忘记在检索集时解包(如果允许的话)mediaRelationship。
还要检查是否设置了反向关系