Swift - Use of unresolved identifier 'self' / Use of unresolved identifier 'Meme' / Use of unresolved identifier 'memedImage'

I get the error message Use of unresolved identifier 'self' as well as the error message Use of unresolved identifier 'Meme'; did you mean 'time'? and Use of unresolved identifier 'memedImage' in Xcode. I am trying to make a MemeMaker and am having trouble creating a struct for the modified image of the meme. Here is my code:

import UIKit


class ViewController: UIViewController, 
UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

    @IBOutlet weak var ImageView: UIImageView!
    @IBOutlet weak var cameraButton: UIBarButtonItem!
    @IBOutlet weak var topTextField: UITextField!
    @IBOutlet weak var bottomTextField: UITextField!

    let textFieldDelegate = TextFieldDelegate()

    let memeTextAttributes:[NSAttributedString.Key: Any] = [
        NSAttributedString.Key.strokeColor: UIColor.black,
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont(name: "HelveticaNeue- 
       CondensedBlack", size: 40)!,
       NSAttributedString.Key.strokeWidth: -3.5]



    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)


        topTextField.textAlignment = .center
        bottomTextField.textAlignment = .center
        topTextField.text = "Top Text"
        bottomTextField.text = "Bottom Text"

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            cameraButton.isEnabled = true
        } else {
            cameraButton.isEnabled = false
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        //
        UIImagePickerController().delegate = self
        //
        self.topTextField.delegate = textFieldDelegate
        self.bottomTextField.delegate = textFieldDelegate
        //
        topTextField.defaultTextAttributes = memeTextAttributes
        bottomTextField.defaultTextAttributes = memeTextAttributes
        //

    }

    //Button to allow user pick an image from an Album
    @IBAction func pickImageFromAlbum(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        present(imagePicker, animated: true, completion: nil)
    }

    //Allows user to use camera to take a photo
    @IBAction func takeImageFromCamera(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        present(imagePicker, animated: true, completion: nil)
    }

    @objc internal func imagePickerController(_ picker: 
UIImagePickerController, didFinishPickingMediaWithInfo info: [String: 
Any]) {
    if let pickedImage =

  info[convertFromUIImagePickerControllerInfoKey
(UIImagePickerController.InfoKey.originalImage)] as? UIImage {

        ImageView.contentMode = .scaleToFill
        ImageView.image = pickedImage
        dismiss(animated: true, completion: nil)

        }
    }
}

func generateMemedImage() -> UIImage {

    // Render view to an image
    UIGraphicsBeginImageContext(self.UIView.frame.size) 
    //Use of unresolved identifier 'self'
    UIView.drawHierarchy(in: self.UIView.frame, afterScreenUpdates: 
true) //Use of unresolved identifier 'self'
    let memedImage:UIImage = 
UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return memedImage
}

func save() {
    // Creates the meme
    let meme = Meme(topText: topTextField.text!, bottomText: 
bottomTextField.text!, originalImage: UIImageView.image!, memedImage: 
memedImage) //Use of unresolved identifier 'memedImage'
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: 
UIImagePickerController.InfoKey) -> String {
    return input.rawValue
}

2 answers

  • answered 2018-10-21 14:38 lokesh

    you have declared function in outside of the class and try to saying that this identifier is belongs to current class but there is no class for that function generateMemedImage()

  • answered 2018-10-21 15:34 matt

    Let's concentrate on just one of the many problems with your code — the issue with self.

    This is the structure of your code. This is your code; all I did here was cut most of it, leaving the structure clearly visible:

    class ViewController: UIViewController, 
        UIImagePickerControllerDelegate, UINavigationControllerDelegate {
            // ...
    }
    
    func generateMemedImage() -> UIImage {
        UIGraphicsBeginImageContext(self.UIView.frame.size)
        // ...
    }
    

    Your class declaration starts — and ends! Then comes a completely new function, func generateMemedImage. It is not inside the class declaration; it is just sitting out in the middle of nowhere. That's not illegal of itself; global functions are legal in Swift. But you cannot say self in a global function.

    So the solution to that problem is to move the function inside the class declaration:

    class ViewController: UIViewController, 
        UIImagePickerControllerDelegate, UINavigationControllerDelegate {
            // ...
            func generateMemedImage() -> UIImage {
                UIGraphicsBeginImageContext(self.UIView.frame.size)
                // ...
            }
    }