【发布时间】:2023-11-02 03:52:01
【问题描述】:
在ActionScript 你可以这样做:
character.x = character.width;
character.y = character.height;
或者你可以简单地这样做:
character.x = this.width;//`this` automagically == `character` in Actionscript (or whichever object is being modified. `this` just points to the current object being modified, it's a built in global dynamic variable)
character.y = this.height;
其中this 是一个动态变量,它仅指向正在编辑参数的对象(在本例中为x 或y)。
..
objectA.property1 = objectA.property2;
完全一样
objectA.property1 = this.property2;
因为this 是一个动态的面向对象的变量,它指向被自动修改的对象。
Objective-C 中有 this 这样的东西吗?
好处是允许我使用 #define 使用 clang 创建快捷方式。
例如,现在我可能想要一个 CLANG (NOT Obj-C) 快捷方式来使对象在其父视图中居中:
#define centerInSuperview center = CGPointMake(this.superview.frame.size.width/2.0f, this.superview.frame.size.height/2.0f)
那我可以说
object1.centerInSuperview;
可以这样说:
object1.center = CGPointMake(this.superview.frame.size.width/2.0f, this.superview.frame.size.height/2.0f);
除了this 不是 Obj-c 中的东西,所以我需要其他东西来指向正在修改的对象,以便我可以制作 CLANG 快捷方式。
【问题讨论】:
-
我认为您不了解
this在 ActionScript 中的工作原理。 -
Objective-C 中
this的等价物是self,但如果您曾经费心阅读初学者的 Objective-C 教程,就会知道这一点。 -
按照我的理解,他不希望
this表示self,他希望它表示character。 -
@TheParamagneticCroissant 这太粗鲁了。我从来没有在这个网站上遇到过像你这样顽固的人,我已经在 Objective-c 中编码了 6 年,这是非常没有必要的。我不是在谈论
self。假设我在一个视图控制器中,我有一个像 UIView 这样的对象,我正在修改它的 .center 值,我可以在代码行中引用它(UIView)而不重复它的名称,而是只是说类似this。 -
@jtbandes 绝对是
this在 ActionScript 中的工作原理,我已经用这种语言编码了 10 多年。
标签: ios objective-c xcode this dynamic-variables