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

标签 ios swift unrecognized-selector

我对 iOS 原生应用的开发有点陌生。我正在使用 Swift 4,当我尝试从我的登录名移动到另一个屏幕时,我生成了以下错误:

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)

下面是涉及的类:

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
    }
}

知道为什么会出现此错误吗?

最佳答案

在崩溃发生时进行调试并检查原因。 你可以这样做:

  1. 在底部的断点导航器中单击加号按钮并添加异常断点

enter image description here

  1. 这将向您的项目添加所有异常断点。

enter image description here

这意味着当您的应用程序崩溃时,将停止在产生崩溃的行上。 然后我们可以弄清楚下一步该做什么。

但是,您经常使用 force unwrap,例如:

let cliente = sender as!客户

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

这样比较好

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

请尽量不要使用!。如果您的应用程序遇到 nil 而不是目标程序将会崩溃。

回答你的问题。您的应用程序在尝试将消息 getTextId 发送到类型为 BanGuayaPrototype.ViewController 的对象时确实崩溃了。

无法识别该选择器。那是你崩溃的根源。 那是怎么发生的? 现在我能想到的。你有一个对象: 让对象:BanGuayaPrototype 而您想要做的是在此对象上调用方法 getTextId

这种类型的错误在 ObjectiveC 中很常见。当您可以尝试向对象发送消息时发生。如果给定对象无法处理消息,程序就会崩溃。

关于ios - 'NSInvalidArgumentException',原因 : '-[BanGuayaPrototype.ViewController getTextId:]: unrecognized selector sent to instance 0x7fe5f69027d0' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48198387/

相关文章:

ios - 最小化 launchOptionsLocation key 发现上的 didFinishLaunchingWithOptions 进程?

ios - iCarousel numberOfItemsInCarousel

swift - 我如何解决此 : UILabel. 文本必须仅从主线程使用

ios - UITableView.visibleCells 未捕获所有可见单元格

ios - 无法识别的选择器发送到实例 ***

ios - UITabBarController setSelectedIndex 性能低下

ios - iOS,RESTKit,如何设置请求超时

swift - 使用 NSFetchedResultsController 对描述符进行排序 - Swift

objective-c - NSString 绘制矩形 :withAttributes: causes 'unrecognized selector sent to instance ...'

objective-c - Objective C 没有抛出足够多的错误?