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
unneeded
V2EX  ›  iDev

关于 Swift 里 Selector 的一个问题

  •  
  •   unneeded · May 27, 2019 · 8925 views
    This topic created in 2718 days ago, the information mentioned may be changed or developed.

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

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

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

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

    20 replies  •  2019-07-18 00:33:17 +08:00
    maxmak
    maxmak
    May 27, 2019
    1
    selector 其实是 Objective-C runtime 的概念, swift5 以后开始会有 自己 runtime
    Elethom
    Elethom
    May 27, 2019 via iPhone
    2
    面包会有的,用 closure 的 API 也会有的。
    luopengfei14
    luopengfei14
    May 27, 2019 via iPhone
    3
    前期的 swift 相当于没有指针的 oc,依赖于 oc 吧……
    unneeded
    unneeded
    OP
    May 27, 2019
    4
    @maxmak @luopengfei14 谢谢,明白为什么一定要加 @objc 了

    @Elethom 谢谢 那就只好等了😂
    fvckDaybyte2
    fvckDaybyte2
    May 28, 2019 via iPhone
    5
    一般直接从 storyboard 拖出来吧,这么常用的东西到现在还是 oc 那一套的话,可能就是鼓励你从 storyboard 拖线?
    lizhuoli
    lizhuoli
    May 28, 2019 via iPhone
    6
    @Elethom 问题是,Closure 能解决纯代码上的用法,解决不了 Storyboard 这种非代码的运行时绑定问题,还是得用一个类似 Selector 的概念(具体名称是什么不重要不然)
    不然也可以抄 Qt,造一个 Signal-Slot,然后 Storyboard 选择
    Elethom
    Elethom
    May 28, 2019 via iPhone
    7
    @lizhuoli
    对 functions 做另一种形式的索引吗……你不说我都忘了还有 IB 这个东西了,太难维护,尤其是多人协作时,好多年没用了。
    unneeded
    unneeded
    OP
    May 28, 2019
    8
    @fvckDaybyte2 可能我是新手吧,我觉得直接写代码比在 sb 里拖按钮加约束方便点🤣🤣
    fvckDaybyte2
    fvckDaybyte2
    May 28, 2019 via iPhone
    9
    @zhaidoudou123 线拖出来是弱引用,你看属性都是 weak,自己写容易把 target 写成强引用,可能造成循环引用吧,如果界面对象还不是重复使用而是频繁新建销毁的,就有可能内存泄漏,一般用代码写界面的我都觉得是大神,谦虚了
    unneeded
    unneeded
    OP
    May 28, 2019 via iPhone
    10
    @fvckDaybyte2
    我只是单纯觉得方便,没考虑过内存泄露的问题🤣
    你这么一说我确实觉得用 sb 拖好一点了,以后有机会再写的时候仔细学一学约束(先把毕设混过去…
    seabirds
    seabirds
    May 31, 2019 via iPhone
    11
    @fvckDaybyte2 target 不用考虑强引用的问题,xib 弱引用是因为控件 nib 文件创建的
    fvckDaybyte2
    fvckDaybyte2
    May 31, 2019 via iPhone
    12
    @seabirds 然后呢?
    seabirds
    seabirds
    Jun 5, 2019 via iPhone
    13
    @fvckDaybyte2 只是发送消息的对象而已
    fvckDaybyte2
    fvckDaybyte2
    Jun 5, 2019
    14
    @seabirds target 对象是不是被 selector 强引用了?如果 target 是 UIViewController 本身,VC 引用 selector, selector 引用 VC,不就是循环引用么?
    fvckDaybyte2
    fvckDaybyte2
    Jun 5, 2019
    15
    @fvckDaybyte2 当然 selector 不一定是强引用 target,具体看实现的代码,我没看过。
    seabirds
    seabirds
    Jun 5, 2019 via iPhone
    16
    @fvckDaybyte2 不是强引用,只是将响应绑定到目标的方法上
    fvckDaybyte2
    fvckDaybyte2
    Jun 5, 2019
    17
    @seabirds 绑定和引用的区别是啥😂,不知道你有没有学过 c 的指针,不懂完全可以理解,但是请不要误导他人……
    xi_lin
    xi_lin
    Jun 10, 2019❤️1
    18
    @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
    fvckDaybyte2
    Jun 10, 2019
    19
    @xi_lin 弱引用咯
    wezzard
    wezzard
    Jul 18, 2019
    20
    @lizhuoli 我覺得學 ECS 架構的事件響應機制也行,就是整體 GUI 的架構要遷移到 ECS 🌚
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2428 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 15:39 · PVG 23:39 · LAX 07:39 · JFK 10:39
    ♥ Do have faith in what you're doing.