Objective-c 符号 ** & +-
就在我认为我已经熟悉 Objective-c 的时候,提到的符号完全把我扔进了兔子洞......
** 双指针??
& 我可以用 &reference 做哪些事情,是 @property?一个完整的对象?奇怪的指针令人眼花缭乱?
± 我在方法声明之前看到了 + 或 - ;我见过 Java 通过手动输入 + 来注释一些数据类型声明,而在 Eclipse 中编译的魔力会将它们更改为 -
我可能会问重复的问题和/或超出我的猜测;感谢您的回答/编辑。
Just when I think I'm getting comfortable with Objective-c the mentioned symbols totally throw me down a rabbit hole...
** a double pointer??
& what sort of things can I do with &reference, is a @property? a full on object? weird pointer razzledazzle?
± I see both a + or a - before method declarations; I've seen Java annotate some datatype declarations by manually typing the + and the magic of compiling in Eclipse would change them to a -
I'm likely asking repetitious questions and/or way outta the ballpark on my guesses; thanks for answers/edits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将进入 Objective-c 构建于其之上的 C 部分。
** 是一个指向指针的指针。由于 C 中的函数按值获取参数,这意味着您无法更改该函数中参数的值。但是,通过提供间接级别并将指针传递给指针,您可以更改该值。
&意味着它是一个参考。如果参数采用 ** 并且您有一个 * 变量,请传递对其的引用。
Objective-C / Cocoa 中的常见用法是 NSError。这本质上是一场争论。
You're getting into the C portion that objective-c is built on top of.
** is a pointer to a pointer. Since functions in C take arguments by value, it means you can't change the value of the argument in that function. But, by providing a level of indirection and passing a pointer to the pointer, you can change the value.
& means it's a reference. If an argument takes a ** and you have a * variable, pass a reference to it.
A common usage in objective-c / cocoa is NSError. It's essentially an out argument.
您可能知道,指针指向对象的地址,并且是引用对象的方式。在 Objective-C 中有时会使用双指针,主要用于返回 NSErrors,如果发生错误,你想取回错误对象(NSError)的地址,即指针,因此你传入一个分配为 null 的指针调用者可以更改该指针,使其指向另一个指针的地址,而另一个指针又指向 NSError 对象。
与号 (&) 主要由较低级别的 C API 使用,例如 Core Graphics。它们用于引用事物,例如当前上下文。只要您的大部分代码在其方法调用周围使用方括号,您就不会经常看到这些。
在方法声明之前使用 + 或 - 用于区分类 (+) 和实例 (-) 方法。类方法在类本身上调用(例如 alloc),而实例方法在该对象的实例上调用(例如 init)。
As you might know, a pointer points to the address of an object and is the way you reference an object. A double pointer is sometimes used in Objective-C, mainly for returning NSErrors, where you want to get back the address, i.e. a pointer, to an error object (NSError) if an error occurred, thus you pass in a pointer assigned to null and the caller can change that pointer so that it points to the address of another pointer which in turn points to an NSError object.
The ampersand (&) is mostly used by the lower level C APIs, e.g. Core Graphics. They are used to reference things, like the current context. As long as most of your code uses square brackets around its method calls you won't see these very often.
Using a + or a - before a method declarations is used to differentiate between class (+) and instance (-) methods. A class methods is called on the class itself (such as alloc), while a instance method is called on an instance of that object (such as init).
方法声明之前的
-
和+
指定实例方法和静态类方法。要使用实例方法,您必须先创建类的对象,然后才能调用其方法,可以直接从类类型调用静态方法-
and+
before a method declaration designate an instance method and a static class method. To use an instance method you have to create an object of your class before you can call its method, a static method can be called directly from a class type