[英]How to add function parameter after selector parameter Swift
I am writing a function with a definition that looks like this我正在写一个 function 的定义看起来像这样
@objc public func artOptionsTapped(_ barButtonItem: UIBarButtonItem, imageReferences: [StorageReference]?)
I am wondering what the proper way to call this function is if I want to pass in a list called images
as the imageReferences
parameter.我想知道调用这个 function 的正确方法是如果我想传入一个名为images
的列表作为imageReferences
参数。 I tried to call it like this:我试着这样称呼它:
#selector(artOptionsTapped(:_, imageReferences: images))
But that's causing me an error.但这导致我出错。 Thanks!谢谢!
Assuming that I'm interpreting your question correctly, you want a selector to artOptionsTapped
where the imageReferences
parameter is always images
.假设我正确解释了您的问题,您需要一个artOptionsTapped
选择器,其中imageReferences
参数始终为images
。 This is partial function application , and is not currently natively supported in Swift.这是部分 function 应用程序,目前在 Swift 中不受本机支持。 To get the equivalent functionality, try this:要获得等效功能,请尝试以下操作:
let images = getImages() // however you get the images
let closure = { (barButtonItem: UIBarButtonItem) in
artOptionsTapped(barButtonItem, images)
}
#selector(closure(_:))
(fair warning, this may not work — I haven't tried it myself and I may not understand the context you're trying this in, but I figured it was worth a shot) (公平的警告,这可能行不通——我自己没有尝试过,我可能不理解你正在尝试这个的上下文,但我认为值得一试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.