简体   繁体   English

您如何知道要提供哪个字符串作为Swift函数的选择器?

[英]How do you know which string to provide as a selector for Swift functions?

Sometimes when I am working with selectors in Swift (ie the Selector type), the string literal that I provide for the action parameter to methods like targetForAction(_:withSender:) or addTarget(_:action:) doesn't invoke or map to the actual Swift function to which I am expecting. 有时,当我在斯威夫特选择工作(即Selector型),字符串文字,我提供了action ,以类似的方法参数targetForAction(_:withSender:)addTarget(_:action:)不会调用或地图到我期望的实际Swift功能。

For example, when an instance of MyResponder as shown below is in the responder chain, it cannot be found when calling targetForAction(_:withSender) using the string showImage: . 例如,当如下所示的MyResponder实例位于响应者链中时,使用字符串showImage:调用targetForAction(_:withSender)targetForAction(_:withSender)showImage: What is the pattern for knowing what is the proper string literal to provide for different types of Swift function signatures and the options for required and/or omitted argument labels? 怎样才能知道为不同类型的Swift函数签名提供正确的字符串文字以及所需和/或省略的参数标签的选项?

import UIKit

class MyResponder: UIResponder {

    func showImage( named filename: String ) {
        print( "Loading image..." )
    }
}

class MyViewController: UIViewController {

    @IBAction func buttonTapped( sender: AnyObject? ) {
        if let responder = self.targetForAction( "showImage:", withSender: self ) as? MyResponder {
            responder.showImage(named: "my_image" )
        }
    }
}

With some trial, error and encouragement from commenters, I managed to figure out the pattern! 经过评论者的反复试验,错误和鼓励,我设法弄清楚了这种模式!

The string literal has to follow Objective-C syntax translated from the signature of your Swift function, which is an interesting tidbit that wasn't obvious at first, but makes perfect sense when you consider the purpose things like the @objc attribute. 字符串文字必须遵循从您的Swift函数的签名转换而来的Objective-C语法,这是一个有趣的@objc并不明显,但在考虑诸如@objc属性之类的有目的的东西时,这是@objc In writing up a better code sample, I seem to have figured out the pattern for the mappings myself. 在编写更好的代码示例时,我似乎已经弄清楚了映射的模式。

functionName + ( With + First ) + : + ( second ) + : etc. functionName +( With + First )+ : +( second )+ :

Where what's in the parenthesis is required only when the argument label is required (ie not omitted). 仅当需要参数标签时(即不省略),才需要括号中的内容。 And remember to capitalize With and First . 并记住要WithFirst

In each of the following examples, myObject will return itself as the target for the provided selector, indicating that the string literal provided as the Selector did in fact map that the Swift function for which it was intended. 在下面的每个示例中, myObject将自身返回为提供的选择器的目标,指示作为Selector提供的字符串文字实际上映射了它打算使用的Swift函数。

import UIKit

class MyObject : UIResponder {
    func someFunction() {}
    func someFunction(param:String) {}
    func someLabeledFunction(param param:String) {}
    func someTwoArgumentFunction(param1:String, param2:String) {}
    func someTwoArgumentNoLabelFunction(param1:String, _ param2:String) {}
    func someHalfLabeledTwoArgumentFunction(param1 param1:String, _ param2:String) {}
    func someCompletelyLabeledTwoArgumentFunction(param1 param1:String, param2:String) {}
}

let myObject = MyObject()
myObject.targetForAction("someFunction", withSender: nil)
myObject.targetForAction("someFunction:", withSender: nil)
myObject.targetForAction("someLabeledFunctionWithParam:", withSender: nil)
myObject.targetForAction("someTwoArgumentFunction:param2:", withSender: nil)
myObject.targetForAction("someTwoArgumentNoLabelFunction::", withSender: nil)
myObject.targetForAction("someHalfLabeledTwoArgumentFunctionWithParam1::", withSender: nil)
myObject.targetForAction("someCompletelyLabeledTwoArgumentFunctionWithParam1:param2:", withSender: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM