@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

原文地址:https://www.cnblogs.com/feng9exe/p/10472366.html

时间: 2024-10-13 20:58:53

@objc vs @objc dynamic官方解释的相关文章

MATLAB &#39; : &#39; 官方解释

1.冒号的作用 产生矢量,阵列标注以及for-loop迭代子 2.描述 冒号是MATLAB中最有用的操作符之一.它使用下述规则来创建有规则的空间矢量: j:k is the same as [j,j+1,...,k], or empty when j > k. j:i:k is the same as [j,j+i,j+2i, ...,j+m*i], where m = fix((k-j)/i), for integer values. For information on the defini

为什么我刚发表的文章变成了“待审核”,csdn有没有官方解释啊

为什么我刚发表的文章变成了"待审核",csdn有没有官方解释啊,什么样的文章才会变为待审核呢? 而且从草稿箱和回收站里也看不到我的文章了,希望我的文章没有删掉. 文章的字是一个个打上去的啊,自己没有备份.. 刚刚测试了一下,貌似带图片的就会自动进入待审核状态,是不是这样子? 为什么我刚发表的文章变成了"待审核",csdn有没有官方解释啊

cocos2d-x 3.0 touch事件官方解释

官方解释 http://www.cocos2d-x.org/docs/manual/framework/native/input/event-dispatcher/zh#_1

Android USER 版本与ENG 版本的差异--MTK官方解释

 分类: Android(4)  Description]Android USER 版本与ENG 版本的差异 [Keyword]USER ENG user eng 用户版本 工程版本 差异 [Solution]Google 官方描述: USER/USERDEBUG/ENG 版本的差异, 参考alps/build/core/build-system.html 的详细说明eng This is the default flavor. A plain make is the same as make

WM_ERASEBKGND官方解释(翻译),以及Delphi里所有的使用情况

#define WM_ERASEBKGND                   0x0014 Parameters wParam A handle to the device context. // 设备上下文的句柄 lParam This parameter is not used. Return value Type: LRESULT An application should return nonzero if it erases the background; otherwise, it

iOS 中 NSParameterAssert 是干啥的之官方解释翻译篇

断言评估一个条件,如果条件为 false ,调用当前线程的断点句柄.每一个线程有它自已的断点句柄,它是一个 NSAsserttionHandler 类的对象.当被调用时,断言句柄打印一个错误信息,该条信息中包含了方法名.类名或函数名.然后,它就抛出一个 NSInternalInconsistencyException 异常.Assertions evaluate a condition and, if the condition evaluates to false, call the asse

iOS NSParameterAssert 是干啥的之官方解释翻译篇

断言评估一个条件,如果条件为 false ,调用当前线程的断点句柄.每一个线程有它自已的断点句柄,它是一个 NSAsserttionHandler 类的对象.当被调用时,断言句柄打印一个错误信息,该条信息中包含了方法名.类名或函数名.然后,它就抛出一个 NSInternalInconsistencyException 异常. Assertions evaluate a condition and, if the condition evaluates to false, call the ass

关于OBJC

http://www.objc.io/ objc这个网站是:关于objective-c语言的最佳实践和高阶技术的期刊. 看了几期很不错,所以计划每天抽空翻译一篇文章和大家一起分享. 关于OBJC,布布扣,bubuko.com

C# 动态语言特性,dynamic 关键字研究

原文:C# 动态语言特性,dynamic 关键字研究 1       动态语言简介 支持动态特性的语言现在大行其道,并且有继续增长的趋势.比如 Ruby 和 Python, 还有天王级的巨星 --- JavaScript. 现在一个程序员说自己对 JavaScript 根本没使用过,别人一定把你当成从火星回来的吧! 很多使用过 JavaScript 的程序员,刚开始对其动态特性深恶痛绝,欲除之而后快,但是一旦熟悉这个语言以后,又会发疯般的爱上她(我的野蛮女友). 以创建一个“人”为例, Java