空方法名称,这实际上做了什么? [英] Empty method name, what does this actually do?

查看:112
本文介绍了空方法名称,这实际上做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习自己的Objective-c和iOS编程,并且由于这个微妙的错误持续了一个小时,我发现自己陷入了非工作代码。
考虑以下代码:

I'm currently learning myself objective-c and iOS programming and found myself stuck with non-working code due to this subtle error for an hour. Consider the following code:

@property (strong, nonatomic) NSString *name;
- (NSString *):name { return @"Some name"; }

乍一看(对于任何新人)这看起来像是一个被覆盖的<$ c的getter $ c> name property。但是有一个非常微妙的,不应该存在。这里没有来自编译器/解析器/运行时的警告/错误,所以我的问题是这实际上最终是什么?

At first glance (and for anyone new) this looks like an overridden getter for the name property. But theres a very subtle : that shouldn't be there. You get no warning/error from the compiler/parser/runtime here, so my question is what does this actually end up as?

我试图找到一种调用方式这个方法一旦我看到错误,但在我的几次尝试中都没有成功。

I tried to figure a way of calling this method once I saw the error, but didn't succeed in my few attempts.

推荐答案

方法签名 - (NSString *):name 分解为以下内容:

The method signature - (NSString *):name breaks down to the following:


  • - 这是一个实例方法(与 + 的类方法相比)。

  • (NSString *)它返回一个字符串。

  • 如果你要说的话这个方法的名称,简称为冒号。 告诉编译器您的方法也接受一个参数。

  • name 有一个名为name的参数。

  • - It is an instance method (versus a class method with a +).
  • (NSString *) It returns a string.
  • : If you were to speak the name of this method, it would simply be called "colon". : tells the compiler that your method accepts one parameter as well.
  • name There is a parameter called name.

当您未指定类型时,编译器假定您的意思是 id ,所以这个方法实际上是 - (NSString *):( id)你好

When you don't specify a type, the compiler assumes you meant id, so this method actually fleshes out to be - (NSString *):(id)hello

有效对此方法的调用将是: [self:@hello]

A valid call to this method would be: [self :@"hello"].

你可以做一些非常奇怪的事情因为是方法的有效名称,编译器假定 id 。如果你真的想,你可以使用一个名为 - ::: 的方法。编译器会假设你的意思是 - (id):( id):( id):( id),一个返回类型对象的方法id 并采用类型 id 的三个参数。你会这样称呼: [self:@hello:anObject:myObject];

You can do really weird things because : is a valid name for a method, and the compiler assumes id. You could, if you really wanted to, have a method called - :::. The compiler would assume you meant - (id):(id):(id):(id), a method that returns an object of type id and takes three parameters of type id. You'd call it like so: [self :@"hello" :anObject :myObject];

这篇关于空方法名称,这实际上做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆