0

I am getting this error "Instance member 'jpegData' cannot be used on type 'UIImage'; did you mean to use a value of this type instead?"

@IBAction func saveButtonWasPressed(_ sender: UIBarButtonItem) {
    if NoteNameLabel.text == "" || NoteNameLabel.text == "NOTE NAME" || noteDescriptionLabel.text == "" || noteDescriptionLabel.text == "Note Description..." {

        let alertController = UIAlertController(title: "Missing Information", message:"You left one or more fields empty. Please make sure that all fields are filled before attempting to save.", preferredStyle: UIAlertController.Style.alert)
        let OKAction = UIAlertAction(title: "Dismiss", style: UIAlertAction.Style.default, handler: nil)

        alertController.addAction(OKAction)

        self.present(alertController, animated: true, completion: nil)

    }

    else {
        if (isExesting == false) {
            let noteName = NoteNameLabel.text
            let noteDescription = noteDescriptionLabel.text

            if let moc = managedObjectContext {
                let note = Note(context: moc)

                if let data: Data = UIImage.jpegData(compressionQuality: 1.0) {
                    note.noteImage = data as NSData as Data
                }

                note.noteName = noteName
                note.noteDescription = noteDescription

                saveToCoreData() {

                    let isPresentingInAddFluidPatientMode = self.presentingViewController is UINavigationController

                    if isPresentingInAddFluidPatientMode {
                        self.dismiss(animated: true, completion: nil)

                    }

                    else {
                        self.navigationController!.popViewController(animated: true)

                    }

                }

            }

        }

        else if (isExesting == true) {

            let note = self.note

            let managedObject = note
            managedObject!.setValue(NoteNameLabel.text, forKey: "noteName")
            managedObject!.setValue(noteDescriptionLabel.text, forKey: "noteDescription")

            if let data: Data = UIImage.jpegData(compressionQuality: 1.0) {
                managedObject!.setValue(data, forKey: "noteImage")
            }

            do {
                try context.save()

I try to use jpegData(compressionQuality:) and pngData() instance methods of the UIImage class like the documentation says I can but still have an error

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AYohannes
  • 625
  • 2
  • 7
  • 18
  • When reviewing the duplicate, note that the 2nd half of the question is the same as yours and the answer fully applies. – rmaddy Nov 14 '18 at 04:23
  • Thank you and I did try that one but it didn't work – AYohannes Nov 14 '18 at 04:41
  • It does work. You must have done it incorrectly. As show, you need to call `jpegData` on an instance of a `UIImage`, not on `UIImage` itself. Of course you actually need to have a `UIImage` instance but there doesn't seem to be one in the code you posted. – rmaddy Nov 14 '18 at 04:45
  • You are right I have to call it . I was confused where the errors comes from for hours. it works now . thanks sir – AYohannes Nov 14 '18 at 04:50
  • FYI - `note.noteImage = data as NSData as Data` should simply be `note.noteImage = data`. It makes no sense to cast `Data` to `NSData` and back to `Data`. Also note that variable names should start with lowercase letters. – rmaddy Nov 14 '18 at 04:51
  • You are correct ! I am still learning swift almost 6 weeks now and I am making lots of mistakes I know but that’s how I learn . Thanks for the help – AYohannes Nov 14 '18 at 05:27

0 Answers0