V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
zhaidoudou123
V2EX  ›  iDev

关于 Swift 里 Selector 的一个问题

  •  
  •   zhaidoudou123 · 2019-05-27 19:55:24 +08:00 · 7511 次点击
    这是一个创建于 1789 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Swift 初学者,以前一直只是用,今天突然想到一个问题怎么也理解不了。

    比如我在设置一个按钮的响应时,常常需要用类似 action: #selector(function) 这样的代码,然后在写 function 具体内容的时候必须要加 @objc。

    经过查阅,我发现加 @objc 的原因是因为使用了 Objective-C Runtime 的消息机制,虽然不太懂,我就当 selector 一定要这么用吧。

    那么问题来了,为什么苹果不为 Swift 增加一个不需要依靠 Objective-C 的响应机制呢? 想知道大家的看法。

    20 条回复    2019-07-18 00:33:17 +08:00
    maxmak
        1
    maxmak  
       2019-05-27 21:13:09 +08:00
    selector 其实是 Objective-C runtime 的概念, swift5 以后开始会有 自己 runtime
    Elethom
        2
    Elethom  
       2019-05-27 21:28:42 +08:00 via iPhone
    面包会有的,用 closure 的 API 也会有的。
    luopengfei14
        3
    luopengfei14  
       2019-05-27 21:58:03 +08:00 via iPhone
    前期的 swift 相当于没有指针的 oc,依赖于 oc 吧……
    zhaidoudou123
        4
    zhaidoudou123  
    OP
       2019-05-27 22:07:19 +08:00
    @maxmak @luopengfei14 谢谢,明白为什么一定要加 @objc

    @Elethom 谢谢 那就只好等了😂
    fvckDaybyte2
        5
    fvckDaybyte2  
       2019-05-28 10:46:42 +08:00 via iPhone
    一般直接从 storyboard 拖出来吧,这么常用的东西到现在还是 oc 那一套的话,可能就是鼓励你从 storyboard 拖线?
    lizhuoli
        6
    lizhuoli  
       2019-05-28 12:54:25 +08:00 via iPhone
    @Elethom 问题是,Closure 能解决纯代码上的用法,解决不了 Storyboard 这种非代码的运行时绑定问题,还是得用一个类似 Selector 的概念(具体名称是什么不重要不然)
    不然也可以抄 Qt,造一个 Signal-Slot,然后 Storyboard 选择
    Elethom
        7
    Elethom  
       2019-05-28 13:30:21 +08:00 via iPhone
    @lizhuoli
    对 functions 做另一种形式的索引吗……你不说我都忘了还有 IB 这个东西了,太难维护,尤其是多人协作时,好多年没用了。
    zhaidoudou123
        8
    zhaidoudou123  
    OP
       2019-05-28 13:44:32 +08:00
    @fvckDaybyte2 可能我是新手吧,我觉得直接写代码比在 sb 里拖按钮加约束方便点🤣🤣
    fvckDaybyte2
        9
    fvckDaybyte2  
       2019-05-28 13:56:39 +08:00 via iPhone
    @zhaidoudou123 线拖出来是弱引用,你看属性都是 weak,自己写容易把 target 写成强引用,可能造成循环引用吧,如果界面对象还不是重复使用而是频繁新建销毁的,就有可能内存泄漏,一般用代码写界面的我都觉得是大神,谦虚了
    zhaidoudou123
        10
    zhaidoudou123  
    OP
       2019-05-28 14:26:46 +08:00 via iPhone
    @fvckDaybyte2
    我只是单纯觉得方便,没考虑过内存泄露的问题🤣
    你这么一说我确实觉得用 sb 拖好一点了,以后有机会再写的时候仔细学一学约束(先把毕设混过去…
    seabirds
        11
    seabirds  
       2019-05-31 01:57:59 +08:00 via iPhone
    @fvckDaybyte2 target 不用考虑强引用的问题,xib 弱引用是因为控件 nib 文件创建的
    fvckDaybyte2
        12
    fvckDaybyte2  
       2019-05-31 08:11:03 +08:00 via iPhone
    @seabirds 然后呢?
    seabirds
        13
    seabirds  
       2019-06-05 17:29:31 +08:00 via iPhone
    @fvckDaybyte2 只是发送消息的对象而已
    fvckDaybyte2
        14
    fvckDaybyte2  
       2019-06-05 18:05:04 +08:00
    @seabirds target 对象是不是被 selector 强引用了?如果 target 是 UIViewController 本身,VC 引用 selector, selector 引用 VC,不就是循环引用么?
    fvckDaybyte2
        15
    fvckDaybyte2  
       2019-06-05 18:08:02 +08:00
    @fvckDaybyte2 当然 selector 不一定是强引用 target,具体看实现的代码,我没看过。
    seabirds
        16
    seabirds  
       2019-06-05 18:33:54 +08:00 via iPhone
    @fvckDaybyte2 不是强引用,只是将响应绑定到目标的方法上
    fvckDaybyte2
        17
    fvckDaybyte2  
       2019-06-05 18:38:52 +08:00
    @seabirds 绑定和引用的区别是啥😂,不知道你有没有学过 c 的指针,不懂完全可以理解,但是请不要误导他人……
    xi_lin
        18
    xi_lin  
       2019-06-10 13:33:50 +08:00   ❤️ 1
    @fvckDaybyte2 先看文档再说

    Summary

    Associates a target object and action method with the control.
    Declaration

    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
    Discussion

    You may call this method multiple times to configure multiple targets and actions for the control. It is also safe to call this method multiple times with the same values for the target and action parameters. The control maintains a list of its attached targets and actions along with the events each supports.
    The control does not retain the object in the target parameter. It is your responsibility to maintain a strong reference to the target object while it is attached to a control.
    Specifying a value of 0 for the controlEvents parameter does not prevent events from being sent to a previously registered target and action method. To stop the delivery of events, always call the removeTarget:action:forControlEvents: method.
    fvckDaybyte2
        19
    fvckDaybyte2  
       2019-06-10 13:40:12 +08:00
    @xi_lin 弱引用咯
    wezzard
        20
    wezzard  
       2019-07-18 00:33:17 +08:00
    @lizhuoli 我覺得學 ECS 架構的事件響應機制也行,就是整體 GUI 的架構要遷移到 ECS 🌚
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2747 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 788ms · UTC 12:28 · PVG 20:28 · LAX 05:28 · JFK 08:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.