Yesid Vargas
Yesid Vargas

Reputation: 43

'NSInvalidArgumentException', reason: '-[BanGuayaPrototype.ViewController getTextId:]: unrecognized selector sent to instance 0x7fe5f69027d0'

I am a bit new in the development of native apps for iOS. I am working on Swift 4 and when trying to move to another screen from my login, I am generating the following error:

BanGuayaPrototype[11289:1143773] -[BanGuayaPrototype.ViewController getTextId:]: unrecognized selector sent to instance 0x7fe5f69027d0 2018-01-10 14:40:06.213350-0500 
BanGuayaPrototype[11289:1143773] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BanGuayaPrototype.ViewController getTextId:]: unrecognized selector sent to instance 0x7fe5f69027d0' 
* First throw call stack: ( 
0 CoreFoundation 0x000000010d42512b exceptionPreprocess + 171 
1 libobjc.A.dylib 0x0000000108ce3f41 objc_exception_throw + 48 
2 CoreFoundation 0x000000010d4a6024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 
3 UIKit 0x0000000109a52f51 -[UIResponder doesNotRecognizeSelector:] + 295 
4 CoreFoundation 0x000000010d3a7f78 ___forwarding_ + 1432 
5 CoreFoundation 0x000000010d3a7958 _CF_forwarding_prep_0 + 120 
6 UIKit 0x0000000109820972 -[UIApplication sendAction:to:from:forEvent:] + 83 
7 UIKit 0x000000010999fc3c -[UIControl sendAction:to:forEvent:] + 67 
8 UIKit 0x000000010999ff59 -[UIControl _sendActionsForEvents:withEvent:] + 450 
9 UIKit 0x000000010a548407 -[UITextField _resignFirstResponder] + 155 
10 UIKit 0x0000000109a52939 -[UIResponder _finishResignFirstResponder] + 286 
11 UIKit 0x000000010a548026 -[UITextField _finishResignFirstResponder] + 48 
12 UIKit 0x0000000109a529e8 -[UIResponder resignFirstResponder] + 140 
13 UIKit 0x000000010a547ef6 -[UITextField resignFirstResponder] + 135 
14 UIKit 0x00000001098d6463 -[UIView(Hierarchy) _removeFirstResponderFromSubtree] + 167 
15 UIKit 0x00000001098d6a73 UIViewWillBeRemovedFromSuperview + 72 
16 UIKit 0x00000001098d685d -[UIView(Hierarchy) removeFromSuperview] + 95 
17 UIKit 0x000000010999b591 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke.674 + 858 
18 UIKit 0x0000000109994b5b -[UIPresentationController transitionDidFinish:] + 111 
19 UIKit 0x0000000109c1cc1e -[_UICurrentContextPresentationController transitionDidFinish:] + 44 
20 UIKit 0x0000000109998f4f __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 183 
21 UIKit 0x000000010a58d088 -[_UIViewControllerTransitionContext completeTransition:] + 102 
22 UIKit 0x0000000109991dba -[UITransitionView notifyDidCompleteTransition:] + 251 
23 UIKit 0x0000000109991a31 -[UITransitionView _didCompleteTransition:] + 1397 
24 UIKit 0x00000001099940c8 -[UITransitionView _transitionDidStop:finished:] + 104 
25 UIKit 0x00000001098b56c4 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 343 
26 UIKit 0x00000001098b5d23 -[UIViewAnimationState animationDidStop:finished:] + 293 
27 UIKit 0x00000001098b5dd7 -[UIViewAnimationState animationDidStop:finished:] + 473 
28 QuartzCore 0x00000001096696bd _ZN2CA5Layer23run_animation_callbacksEPv + 323 
29 libdispatch.dylib 0x000000010e54733d _dispatch_client_callout + 8 
30 libdispatch.dylib 0x000000010e5525f9 _dispatch_main_queue_callback_4CF + 628 
31 CoreFoundation 0x000000010d3e7e39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9 
32 CoreFoundation 0x000000010d3ac462 __CFRunLoopRun + 2402 
33 CoreFoundation 0x000000010d3ab889 CFRunLoopRunSpecific + 409 
34 GraphicsServices 0x0000000110ff39c6 GSEventRunModal + 62 
35 UIKit 0x000000010981f5d6 UIApplicationMain + 159 
36 BanGuayaPrototype 0x00000001083a31d7 main + 55 
37 libdyld.dylib 0x000000010e5c3d81 start + 1 
38 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Below are the classes involved:

ViewController.swift

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var getText: UITextField!
    var cliente = Cliente()

    @IBAction func onGoButtonLH(_ sender: Any) {
        let idTxt:String? = getText.text

        if idTxt == "1234" {
            self.performSegue(withIdentifier: "LoginToComercial", sender: self)
        }else{
            let urlString = "http://localhost:8080/RestServiceBgPrototype/cognitiva/clientService/"+idTxt!
            guard let url = URL(string: urlString) else { return }

            URLSession.shared.dataTask(with: url) { (data, response, error) in
                if error != nil {
                    print(error!.localizedDescription)
                }

                guard let data = data else { return }
                //Implement JSON decoding and parsing
                do {
                    let decoder = JSONDecoder()
                    self.cliente = try! decoder.decode(Cliente.self, from: data)
                    OperationQueue.main.addOperation{
                        self.performSegue(withIdentifier: "LoginToHome", sender: self.cliente)
                    }
                }
                }.resume()
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "LoginToHome") {
            let homeViewController = segue.destination as? HomeViewController
            let cliente = sender as! Cliente
            homeViewController?.cliente = cliente
        }
    }
}

HomeViewController.swift

class HomeViewController: UIViewController {
    @IBOutlet weak var userLabel: UILabel!
    var cliente = Cliente()

    override func viewDidLoad() {
        super.viewDidLoad()

        let nombreCliente:String = cliente.nombre
        let fullNameArr = nombreCliente.components(separatedBy:(" "))
        let nombre    = fullNameArr[2]
        let apellido = fullNameArr[0]
        self.userLabel.text = "Hola, "+nombre.capitalized+" "+apellido.capitalized
    }
}

Any idea why I get this error?

Upvotes: 1

Views: 968

Answers (1)

Shial
Shial

Reputation: 1406

To debug crash while it's happening and inspect what is the reason. You can do this:

  1. In your Breakpoint navigator at the bottom click plus button and add exception breakpoint

enter image description here

  1. That will add to your project all exception break point.

enter image description here

It means when ever your app crashes is will stop on a line which generate crash. Then we can figure out what to do next.

However, you are using force unwrap a lot, for instance:

let cliente = sender as! Cliente

if error != nil {
    print(error!.localizedDescription)
}

It's better to do like this

if let error = error {
    print(error.localizedDescription)
}

Please try not to use !. In case if your application encounter nil instead of object program will crash.

To answer your question. Your application do crash trying to send message getTextId to an object of type BanGuayaPrototype.ViewController.

This selector is not recognised. That's the root of your crash. How did that happen? For now what I can think. You have an object: let obj: BanGuayaPrototype and what are you trying to do is to call a method getTextId on this object.

This type of error is common in ObjectiveC. Occurs when you can try to send message to an object. If given object are unable to handle message program is crashing.

Upvotes: 1

Related Questions