Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

ios - Strongly typed selectors in swift

I wonder can we implement strongly typed selectors in swift? For example if i have a method named buttonTapped(sender: AnyObject) in my view controller later when we add that method as target to some button can we just say

button.addTarget(self, selector:ViewController.buttonTapped(self), forControlEvents: .TouchUpInside)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Outdated. See Claus J?rgensen's answer for Swift 2.2+

There is no concept of "selector" in Swift. Ideally, closure should be used for such purpose.

What you really want is something like this

button.addAction(forControlEvents: .TouchUpInside) {
   // watch out for retain cycle, use weak or unowned accordingly
    ViewController.buttonTapped(self)
}

and you can have it with this code (untested, but it should give you a start point)

public class ClosureWrapper : NSObject
{
    let _callback : Void -> Void
    init(callback : Void -> Void) {
        _callback = callback
    }
    
    public func invoke()
    {
        _callback()
    }
}

var AssociatedObjectHandle: UInt8 = 0

extension UIControl
{
    public func addAction(forControlEvents events: UIControlEvents, withCallback callback: Void -> Void)
    {
        let wrapper = ClosureWrapper(callback)
        addTarget(wrapper, action:"invoke", forControlEvents: events)
        // as @newacct said in comment, we need to retain wrapper object
        // this only support 1 target, you can use array to support multiple target objects
        objc_setAssociatedObject(self, &AssociatedObjectHandle, wrapper, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
    }
}

and hopefully in the future release of SDK, similar methods takes closure instead of selectors will be added by Apple, so we don't need to implement them ourself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...