目标C语法
我是 Objective-C 编程新手。在许多教程中,我一直看到这种格式的代码:
[[classname function] function];
例如:
[[NSString alloc] initwithformat:parameters];
当我学习 plist 和字典时,我看到了这行代码:
[[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"];
我相信它遵循语法: [[objectname function] function]
有人可以解释一下这个语法是如何工作的以及第一个和第二个示例之间的区别吗?
此外,如果您能为这些示例提供 C 或 Java 中的等效语句,那将会非常有帮助。
I am new to Objective-C programming. In many tutorials I have been seeing code with this format:
[[classname function] function];
For example:
[[NSString alloc] initwithformat:parameters];
When I was learning about plist and dictionaries I saw this line of code:
[[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"];
I believe it follows the syntax: [[objectname function] function]
Can someone please explain how this syntax works and the difference between the first and second example.
Also it would be really helpful if you can provide the equivalent statements in C or Java for these examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
[ClassName methodName]
是调用类方法的方式。 Java 中的等效项是Classname.methodName();
[object methodName]
是调用普通实例方法的方式。 Java 中的等效项是object.methodName();
在 Objective C 中,您还可以在
[]
括号中“嵌套”调用。[[Classname method1] method2]
相当于调用类方法method1
,它返回一些对象,然后然后调用method2 在该对象上。
Java 中的等效项是
Classname.method1().method2();
另请注意,在严格的 Objective C 术语中,您不是“调用对象上的方法”,而是“向一个对象'代替。同样的事情,不同的说法。
[ClassName methodName]
is how you call a class method. The equivalent in Java isClassname.methodName();
[object methodName]
is how you call a normal instance method. The equivalent in Java isobject.methodName();
In Objective C you can also 'nest' calls in
[]
brackets.[[Classname method1] method2]
is equivalent to calling the class methodmethod1
, which returns some object and then callingmethod2
on that object.The equivalent in Java would be
Classname.method1().method2();
Also note that in strict Objective C terminology you don't 'call a method on an object', you 'send a message to an object' instead. Same thing, different words.
它只是一种避免创建临时变量或对象的速记符号。您可以根据需要嵌套任意数量的代码,但应保持代码的可读性。
例如,如果您有一个图像名称字符串数组,要获取图像,您可以这样做:
如果您不想使用
NSString
变量,您可以编写:Clear?
It is simply a shorthand notation that avoids creating transitory variables or objects. You can nest as many of these as you want, but you should keep the code readable.
E.g. if you have an array of strings that are image names, to get the image you could do this:
If you want to do without the
NSString
variable, you can write:Clear?
[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"];基本上是这样做的:
数组中有一个字典(数组是 objname),并且您使用 objectAtIndex 来定位字典。接下来,字典包含许多键和这些键的值。您可以通过执行 objectForKey:id 来定位一个值。
稍微更长一些、更简单的语法是:
[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"]; is basically doing this:
There's a dictionary in an array (array being objname) and you target the dictionary with objectAtIndex. Next the dictionary contains a number of key's and value's for those keys. You target a value by doing objectForKey:id.
Somewhat lengthier simpler syntax would be:
好的,首先要做的事情。
[[obj method1] method2]
变体将method1
消息发送到obj
,然后将method2
消息发送到[obj method1]
的返回值。第二个示例
[[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"]
稍微复杂一些。self.objname
是对对象self
的属性objname
的访问(self 始终指自己的对象实例)。因此,使用[self.objname objectAtIndex:indexPath.row]
消息objectAtIndex
被发送到self.objname
返回的对象,参数为indexPath.row
。indexPath
再次是一个对象,您可以访问属性row
。在该返回值上,您发送带有参数@"somename"
的消息objectForKey
。 @"str" 是 Objective-C 字符串的内置语法。华泰
Ok, first things first. The
[[obj method1] method2]
variant sends themethod1
message toobj
and then sends themethod2
message to the return value of[obj method1]
.The second example,
[[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"]
is a little bit more complex.self.objname
is an access to the propertyobjname
of the objectself
(self refers always to the own object instance). So with[self.objname objectAtIndex:indexPath.row]
the messageobjectAtIndex
is send to the object returned byself.objname
, with the parameterindexPath.row
.indexPath
is, again, an object and you access the propertyrow
. And on THAT return value you send the messageobjectForKey
with the parameter@"somename"
. And @"str" is the built-in syntax for objective-c strings.HTH
这 。 format 是 get 方法的简写。因此,例如,如果一个 person 对象有年龄,并且您定义了(或合成)了 getAge 方法,则可以调用 [person getAge] 或 person.age 并获得等效的结果。
The . format is a shorthand for a get method. So, for example, if a person object has an age, and you have a getAge method defined (or synthesized), you can call [person getAge] or person.age and get equivalent results.
一般来说,括号意味着您正在向对象发送消息。如果消息返回的值也是一个对象,您也可以向该对象发送消息。第一个示例的内部消息将消息
alloc
发送到类对象NSString
。这将返回一个指向 NSString 类型的已分配对象的指针。然后initWithFormat:
消息初始化字符串的内容。在 Java 中,这将类似于:
在其他文件中:
但 Java 的对象模型与 Objective-C 并不完全相同,因此这只是一个近似值。
In general, the brackets mean that you are sending a message to an object. If the message returns a value that is also an object, you can also send that object a message. The inner message of your first example sends the message
alloc
to the class objectNSString
. This returns a pointer to an allocated object of typeNSString
. TheinitWithFormat:
message then initializes the contents of the string.In Java, this would be something like:
and in some other file:
but Java's object model is not quite the same as Objective-C, so this is only an approximation.
我假设您的困惑在于点表示法,因为这经常让新开发人员感到困惑。这是该语言的一个尴尬且最近添加的内容。我还将假设 mkb 的编辑是正确的,因为您的原始代码不合法。
此调用:
与此调用完全相同:
在像 C++ 这样的语言中,它可能是等效的:
这是一个粗略等效,因为 ObjC 和 C++ 对于如何调用方法有不同的方法,但在大多数情况下,这些将是相同。
您可能对以其他背景学习 Cocoa 感兴趣。我还强烈推荐 Objective-C 编程语言 作为一个开始,或者一本关于这个主题的扎实的书。 Objective-C 与 Java 和 C++ 有很大不同,根据我的经验,尝试边学边学会导致以后出现严重的混乱和挫败感。最好用语言本身的术语来学习这门语言,而不是试图弄清楚“我如何在 ObjC 中做 C++ 的事情?”一半的时间答案是:你不知道。
I will assume that your confusion is over dot-notation, since this often confuses new developers. It was an awkward and recent addition to the language. I will also assume that mkb's edits are correct, since your original code wasn't legal.
This call:
Is precisely the same as this call:
It's rough likely equivalent in a language like C++ would be this:
This is a rough equivalence, because ObjC and C++ have different approaches to how methods are called, but in most cases these would be the same.
You may be interested in Learning Cocoa with other backgrounds. I also strongly recommend The Objective-C Programming Language as a start, or a solid book on the subject. Objective-C is very different from Java and C++, and in my experience, trying to pick it up as you go will lead to significant confusion and frustration later. It is best to learn the language in its own terms rather than trying to figure out "how do I do this C++ thing in ObjC?" Half the time the answer is: you don't.