I am trying to create a sign up page for my app. But I am getting an error when I run the app.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean select:]: unrecognized selector sent to instance 0x10ad5a690'
What is wrong with my code and what it mean?
Here is the my code:
import UIKit
class SignupViewController: UIViewController {
@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
let imagePicker = UIImagePickerController()
var selectedPhoto: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: true, action: #selector(SignupViewController.select(_:)))
tap.numberOfTapsRequired = 1
profileImage.addGestureRecognizer(tap)
}
func selectPhoto(tap:UITapGestureRecognizer) {
self.imagePicker.delegate = self
self.imagePicker.allowsEditing = true
if UIImagePickerController.isSourceTypeAvailable(.camera) {
self.imagePicker.sourceType = .camera
}else{
self.imagePicker.sourceType = .photoLibrary
}
self.present(imagePicker, animated: true, completion: nil)
}
@IBAction func CancelDidTapped(_ sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
@IBAction func RegisterDidTapped(_ sender: AnyObject) {
}
}
extension SignupViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
//ImagePicker
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
selectedPhoto = info[UIImagePickerControllerEditedImage] as? UIImage
self.profileImage.image = selectedPhoto
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
The error indicates that you're incorrectly calling your select(_:)
function on a boolean
:
'-[__NSCFBoolean select:]: unrecognized selector sent to instance 0x10ad5a690'
After examining your code to see where and how you've called select(_:)
, it becomes clear that the problem is that you're setting your UITapGestureRecognizer
's target to a boolean, i.e. true
:
let tap = UITapGestureRecognizer(target: true, action: #selector(SignupViewController.select(_:)))
when it should be set to your function's view controller. For example, in this case, you probably want to set your target to self
:
let tap = UITapGestureRecognizer(target: self, action: #selector(SignupViewController.select(_:)))
As for the select(_:)
method you're calling, it seems to me that you made a typo and that you meant to call the selectPhoto(tap:)
method you've created instead; in which case, your tap gesture declaration and initialization should instead be:
let tap = UITapGestureRecognizer(target: self,
action: #selector(SignupViewController.selectPhoto(tap:)))