1

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?

1 Answer 1

1

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])
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.