@objc vs @objc dynamic官方解释
Some Objective-C APIs—like target-action—accept method or property names as parameters, then use those names to dynamically call or access the methods or properties. In Swift, you use the #selector and #keyPath expressions to represent those method or property names as selectors or key paths, respectively.
https://developer.apple.com/documentation/swift/using_objective-c_runtime_features_in_swift
一、@objc应用于函数是为了能够让函数表达为 #selector;
In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, Objective-C selectors are represented by the Selector structure, and you create them using the #selector expression.
import UIKit
class MyViewController: UIViewController {
let myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
let action = #selector(MyViewController.tappedButton)
myButton.addTarget(self, action: action, forControlEvents: .touchUpInside)
}
@objc func tappedButton(_ sender: UIButton?) {
print("tapped button")
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
二、@objc应用于变量是为了能够让变量表达为keypath字符串,进而使用kvc功能。
class Person: NSObject {
@objc var name: String
@objc var friends: [Person] = []
@objc var bestFriend: Person? = nil
init(name: String) {
self.name = name
}
}
let gabrielle = Person(name: "Gabrielle")
let jim = Person(name: "Jim")
let yuanyuan = Person(name: "Yuanyuan")
gabrielle.friends = [jim, yuanyuan]
gabrielle.bestFriend = yuanyuan
#keyPath(Person.name)
// "name"
gabrielle.value(forKey: #keyPath(Person.name))
// "Gabrielle"
#keyPath(Person.bestFriend.name)
// "bestFriend.name"
gabrielle.value(forKeyPath: #keyPath(Person.bestFriend.name))
// "Yuanyuan"
#keyPath(Person.friends.name)
// "friends.name"
gabrielle.value(forKeyPath: #keyPath(Person.friends.name))
三、@objc dynamic应用于变量是为了让变量能够使用kvo机制。
class MyObjectToObserve: NSObject {
@objc dynamic var myDate = NSDate(timeIntervalSince1970: 0) // 1970
func updateDate() {
myDate = myDate.addingTimeInterval(Double(2 << 30)) // Adds about 68 years.
}
}
class MyObserver: NSObject {
@objc var objectToObserve: MyObjectToObserve
var observation: NSKeyValueObservation?
init(object: MyObjectToObserve) {
objectToObserve = object
super.init()
observation = observe(
\.objectToObserve.myDate,
options: [.old, .new]
) { object, change in
print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
}
}
}
let observed = MyObjectToObserve()
let observer = MyObserver(object: observed)
observed.updateDate() // Triggers the observer's change handler.
// Prints "myDate changed from: 1970-01-01 00:00:00 +0000, updated to: 2038-01-19
Here’s the least you need to remember:
- @objc makes things visible to Objective-C code. You might need this for setting up target/action on buttons and gesture recognizers.
- dynamic opts into dynamic dispatch. You might need this for KVO support or if you‘re doing method swizzling.
- The only way to do dynamic dispatch currently is through the Objective-C runtime, so you must add @objc if you use dynamic.
https://www.cnblogs.com/feng9exe/p/9460336.html
@objc vs @objc dynamic官方解释的更多相关文章
- 零宽度正预测先行断言是什么呢,看msdn上的官方解释定义
最近为了对html文件进行源码处理,需要进行正则查找并替换.于是借着这个机会把正则系统地学一下,虽然以前也用过正则,但每次都是临时学一下混过关的.在学习的过程中还是遇到不少问题的,特别是零宽断言(这里 ...
- 为什么我刚发表的文章变成了“待审核”,csdn有没有官方解释啊
为什么我刚发表的文章变成了"待审核",csdn有没有官方解释啊,什么样的文章才会变为待审核呢? 并且从草稿箱和回收站里也看不到我的文章了,希望我的文章没有删掉. 文章的字是一个个打 ...
- @Resource注解的官方解释
一.@Resource注解的官方解释@Resource annotation, which is semantically defined to identify a specific target ...
- SDE在64位Server2008下Post启动服务失败官方解释
解决了一个SDE启动问题,在此记录一下 在server 2008 64位下安装完arcgis sde之后,Post启动服务,总是失败 查看SDE日志(etc目录下) DB_open_instance( ...
- cocos2d-x 3.0 touch事件官方解释
官方解释 http://www.cocos2d-x.org/docs/manual/framework/native/input/event-dispatcher/zh#_1
- WM_ERASEBKGND官方解释(翻译),以及Delphi里所有的使用情况(就是绘制窗口控件背景色,并阻止进一步传递消息)
#define WM_ERASEBKGND 0x0014 Parameters wParam A handle to the device context. // ...
- MATLAB ' : ' 官方解释
1.冒号的作用 产生矢量,阵列标注以及for-loop迭代子 2.描述 冒号是MATLAB中最有用的操作符之一.它使用下述规则来创建有规则的空间矢量: j:k is the same as [j,j+ ...
- Android USER 版本与ENG 版本的差异--MTK官方解释
分类: Android(4) Description]Android USER 版本与ENG 版本的差异 [Keyword]USER ENG user eng 用户版本 工程版本 差异 [Solu ...
- zabbix3.4.7官方解释触发器
函数 描述 参数 说明 abschange 最近获取值与之前获取值差的绝对值. 支持值的类型: float, int, str, text, log 例如: (最近获取值;之前获取值=ab ...
随机推荐
- [转] ASP.NET MVC 模型绑定的功能和问题
摘要:本文将与你深入探究 ASP.NET MVC 模型绑定子系统的核心部分,展示模型绑定框架的每一层并提供扩展模型绑定逻辑以满足应用程序需求的各种方法. 同时,你还会看到一些经常被忽视的模型绑定技术, ...
- MySQL5:触发器
什么是触发器 MySQL的触发器(trigger)和存储过程一样,都是嵌入到MySQL中的 一段程序.触发器是由事件来触发某个操作,这些事件包括INSERT.UPDATE和DELETE语句.如果定义了 ...
- O(∩_∩)O~~
1.在一切ac的路上,所以的难题都是纸老虎. 2.加油吧,少年.
- Executor、ExecutorService、ThreadPoolExecutor
1.Executor Executor接口中中只有一个方法 执行已提交的Runnable任务对象. ExecutorService pool1 = Executors.newFixedThreadPo ...
- C#的字节与流
计算机中文件有很多种,我们知道实际存在计算机中的都是二进制.这里我记录了通过流对文件的读取操作. 一.首先在这里简单涉及下位,字节,字符的概念. 位(bit):可以表示0或1: 字节(byte):由8 ...
- Code Signal_练习题_isLucky
Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if t ...
- Code Signal_练习题_matrixElementsSum
After they became famous, the CodeBots all decided to move to a new building and live together. The ...
- Layabox 3D游戏开发学习笔记---射线检测,鼠标控制物体运动
核心要点:3D物体碰撞是靠射线检测,射线与碰撞器相撞获取对应的碰撞点信息. class RayPicking03 { private ray: Laya.Ray; private point: Lay ...
- bzoj P4825 [Hnoi2017]单旋——solution
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据 结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的 ...
- 依赖注入(一)构造函数注入(PHP)
构造函数注入(constructor injection)是依赖注入最常见的形式之一. 由名称可以看出,该技术需要我们把所有依赖显示的体现在构造函数中. 好了,直接上代码: <?php /** ...