- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如 Apple 指南中所述,我无法理解“选择器”的部分功能。我把我感到困惑的部分加粗了:
In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it’s used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL. All methods with the same name have the same selector. You can use a selector to invoke a method on an object—this provides the basis for the implementation of the target-action design pattern in Cocoa.
Methods and Selectors For efficiency, full ASCII names are not used as method selectors in compiled code. Instead, the compiler writes each method name into a table, then pairs the name with a unique identifier that represents the method at runtime. The runtime system makes sure each identifier is unique: No two selectors are the same, and all methods with the same name have the same selector.
谁能解释一下这些位?另外,如果不同的类有同名的方法,它们是否也有相同的选择器?
最佳答案
所有选择器都是唯一的——无论是在编译时还是动态地在运行时通过 sel_getUid()
或首选的 sel_registerName()
(后者在很大程度上是首选,前者由于历史原因仍然存在)——为了速度。
背景故事:要调用一个方法,运行时需要一个选择器来标识要调用的内容以及将调用它的对象。这就是为什么 Objective-C 中的每个方法调用都有两个参数:明显且广为人知的 self
和不可见的隐含参数 _cmd
。 _cmd
是当前执行的方法的 SEL。也就是说,您可以将此代码粘贴到任何方法中以查看当前正在执行的方法的名称(选择器):
NSLog(@"%@", NSStringFromSelector(_cmd));
请注意 _cmd
不是全局的;这真的是你的方法的一个论点。见下文。
通过唯一化选择器,所有基于选择器的操作都是使用指针相等性测试而不是字符串处理或任何指针取消引用来实现的。
特别是每次调用方法时:
[someObject doSomething: toThis withOptions: flags]; // calls SEL doSomething:withOptions:
编译器生成此代码(或非常密切相关的变体):
objc_msgSend(someObject, @selector(doSomething:withOptions:), toThis, flags);
objc_msgSend()
做的第一件事是检查 someObject
是否为 nil,如果是则短路 (nil-eats-message)。接下来(忽略标记指针)是在 someObject
的类中查找选择器(实际上是 isa
指针),找到实现 ,并调用它(使用尾调用优化)。
找到实现 的事情必须要快,要让它变得非常快,您希望找到方法实现的关键是尽可能快速和稳定。要做到这一点,您希望 key 可以直接使用并且在全局范围内对于流程是唯一的。
因此,选择器是唯一的。
它也恰好可以节省内存,这是一个了不起的好处,但是如果可以将消息发送速度提高 2 倍(但不是 10 倍是 2 倍——甚至是 2 倍内存是 2 倍速度——),信使将使用比现在更多的内存——虽然速度很关键,但内存使用当然也很关键。
如果你真的想深入了解 objc_msgSend()
的工作原理,我写了一个 bit of a guide .请注意,它有点过时,因为它是在标记指针、 block 实现和 ARC 被公开之前编写的。我应该更新文章。
关于objective-c - 了解 Objective-C 中选择器的唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11051528/
我遵循了一本名为“Sitepoint Full Stack Javascript with MEAN”的书中的教程,我刚刚完成了第 6 章,应该已经创建了一个带有“数据库”的“服务器”。数据库只不过是
在 Jquery 中,我创建两个数组,一个嵌入另一个数组,就像这样...... arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arra
这个问题在这里已经有了答案: What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wa
我被放在别人的代码上,有一个类用作其他组件的基础。当我尝试 ng serve --aot(或 build --prod)时,我得到以下信息。 @Component({ ...,
我正在测试一些代码,并使用数据创建了一个 json 文件。 问题是我在警报中收到“[object Object],[object Object]”。没有数据。 我做错了什么? 这是代码:
我想打印 [object Object],[object Object] 以明智地 "[[{ 'x': '1', 'y': '0' }, { 'x': '2', 'y': '1' }]]"; 在 ja
我有一个功能 View ,我正在尝试以特殊格式的方式输出。但我无法让列表功能正常工作。 我得到的唯一返回是[object Object][object Object] [object Object]
在使用优秀的 Sim.js 和 Three.js 库处理 WebGL 项目时,我偶然发现了下一个问题: 一路走来,它使用了 THREE.Ray 的下一个构造函数: var ray = new THRE
我正在使用 Material UI 进行多重选择。这是我的代码。 {listStates.map(col => (
我的代码使用ajax: $("#keyword").keyup(function() { var keyword = $("#keyword").val(); if (keyword.
我遇到了下一个错误,无法理解如何解决它。 Can't resolve all parameters for AuthenticationService: ([object Object], ?, [o
我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。 这是组件的代码: import { Component, OnInit } from '@angul
我正在开发 NodeJs 应用程序,它是博客应用程序。我使用了快速验证器,我尝试在 UI 端使用快速闪存消息将帖子保存在数据库中之前使用闪存消息验证数据,我成功地将数据保存在数据库中,但在提交表单后消
我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数
我继承了一个非常草率的项目,我的任务是解释为什么它不好。我注意到他们在整个代码中都进行了这样的比较 (IQueryable).FirstOrDefault(x => x.Facility == fac
我只是在删除数组中的对象时偶然发现了这一点。 代码如下: friends = []; friends.push( { a: 'Nexus', b: 'Muffi
这两个代码片段有什么区别: object = nil; [object release] 对比 [object release]; object = nil; 哪个是最佳实践? 最佳答案 object
我应该为其他人将从中继承的第一个父对象传递哪个参数,哪个参数更有效 Object.create(Object.prototype) Object.create(Object) Object.creat
我在不同的对象上安排不同的选择器 [self performSelector:@selector(doSmth) withObject:objectA afterDelay:1]; [self per
NSLog(@"%p", &object); 和 NSLog(@"%p", object); 有什么区别? 两者似乎都打印出一个内存地址,但我不确定哪个是对象的实际内存地址。 最佳答案 这就是我喜欢的
我是一名优秀的程序员,十分优秀!