Migrating my code to Swift 2.2, I have a property var activeTextField:UITextfield?
and the selector I was using was "setActiveTextField:"
. This method does not exist explicitly in my swift code.
With the new syntax, #selector(setActiveTextField)
doesn't work: Use of unresolved identifier
I know I could use Selector("setActiveTextField:")
but I'd loose the benefit of the new swift selectors.
So, what's the new way of doing this?
The issue here is that you're working with a property, not a method. This has two problems:
#selector
expression requires a Swift function/method reference.When Swift compiles your source, it doesn't know that the ObjC -(UITextField*)activeTextField
and -(void)setActiveTextField:(UITextField*)field
methods will exist, so it can't generate function references for them. Since it can't generate a function reference, it doesn't have something it can use for the #selector
expression.
For now, there isn't a way to use #selector
to access property getters/setters — using Selector(...)
with a string constant is your only option.
(This is actually just a new face on a longstanding known issue... it's the same reason you also can't pass a property getter/setter to something like map
or filter
. I think I've seen something on http://bugs.swift.org about this, but I'm not finding it at the moment.)
In Swift 3, SE-0064 was accepted to solve this problem. Now, you would generate that setter like so:
#selector(setter: activeTextField)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With