J.Chang
J.Chang

Reputation: 35

Cannot invoke initializer for type 'NSSet' with an argument list of type '(array: [NSData?])'

Getting this error when updating to swift 2.0:
Cannot invoke initializer for type 'NSSet' with an argument list of type '(array: [NSData?])'

I already do the searching but still can't fix it.

var object:UIImageView = UIImageView(frame: CGRectMake(0, 0, result.size.width, result.size.height))
object.image = UIImage(named: "image_default")
let element = NSEntityDescription.insertNewObjectForEntityForName("XKIMAGEVIEW", inManagedObjectContext: CoreDataUtil.sharedInstance.managedObjectContext!) as! XKIMAGEVIEW
element.image = NSSet(array:[UIImageJPEGRepresentation(object.image!, 1)])

the last line just have the error
seems like NSData couldn't be included in the array

Any idea of how to fix this issue?

Upvotes: 1

Views: 936

Answers (1)

vadian
vadian

Reputation: 285150

UIImageJPEGRepresentation returns an optional, NSSet doesn't accept optionals.
Just unwrap the optional:

NSSet(array:[UIImageJPEGRepresentation(object.image!, 1)!])

Or, if the image representation could be nil use optional bindings

if let imageData = UIImageJPEGRepresentation(object.image!, 1) {
  element.image = NSSet(array:[imageData])
}

Upvotes: 1

Related Questions