目标C语法

发布于 2024-12-12 07:16:49 字数 479 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

鸵鸟症 2024-12-19 07:16:49

[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 is Classname.methodName();

[object methodName] is how you call a normal instance method. The equivalent in Java is object.methodName();

In Objective C you can also 'nest' calls in [] brackets. [[Classname method1] method2] is equivalent to calling the class method method1, which returns some object and then calling method2 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.

安稳善良 2024-12-19 07:16:49

它只是一种避免创建临时变量或对象的速记符号。您可以根据需要嵌套任意数量的代码,但应保持代码的可读性。

例如,如果您有一个图像名称字符串数组,要获取图像,您可以这样做:

NSString *imageName = [imageArray objectAtIndex:0];
UIImage *image = [UIImage imageNamed:imageName];

如果您不想使用 NSString 变量,您可以编写:

UIImage *image = [UIImage imageNamed:[imageArray onjectAtIndex:0]];

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:

NSString *imageName = [imageArray objectAtIndex:0];
UIImage *image = [UIImage imageNamed:imageName];

If you want to do without the NSString variable, you can write:

UIImage *image = [UIImage imageNamed:[imageArray onjectAtIndex:0]];

Clear?

帝王念 2024-12-19 07:16:49

[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"];基本上是这样做的:

数组中有一个字典(数组是 objname),并且您使用 objectAtIndex 来定位字典。接下来,字典包含许多键和这些键的值。您可以通过执行 objectForKey:id 来定位一个值。

稍微更长一些、更简单的语法是:

 NSDictionary *dict = [objname objectAtIndex:indexPath.row];
 NSLog(@"%@", [dict objectForKey@"key"];

[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:

 NSDictionary *dict = [objname objectAtIndex:indexPath.row];
 NSLog(@"%@", [dict objectForKey@"key"];
我很坚强 2024-12-19 07:16:49

好的,首先要做的事情。 [[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.rowindexPath 再次是一个对象,您可以访问属性 row。在该返回值上,您发送带有参数 @"somename" 的消息 objectForKey。 @"str" 是 Objective-C 字符串的内置语法。

华泰

Ok, first things first. The [[obj method1] method2] variant sends the method1 message to obj and then sends the method2 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 property objname of the object self (self refers always to the own object instance). So with [self.objname objectAtIndex:indexPath.row] the message objectAtIndex is send to the object returned by self.objname, with the parameter indexPath.row. indexPath is, again, an object and you access the property row. And on THAT return value you send the message objectForKey with the parameter @"somename". And @"str" is the built-in syntax for objective-c strings.

HTH

夏了南城 2024-12-19 07:16:49

这 。 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.

硪扪都還晓 2024-12-19 07:16:49

一般来说,括号意味着您正在向对象发送消息。如果消息返回的值也是一个对象,您也可以向该对象发送消息。第一个示例的内部消息将消息 alloc 发送到类对象 NSString。这将返回一个指向 NSString 类型的已分配对象的指针。然后 initWithFormat: 消息初始化字符串的内容。

在 Java 中,这将类似于:

public class NSString {
  public static NSString alloc() {
    return new byte[<size of a string>];
  }
  public void initWithParameters(Map<String, String> params) {
    /* initialize */
  }
}

在其他文件中:

NSString str = NSString.alloc().initWithParameters(params);

但 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 object NSString. This returns a pointer to an allocated object of type NSString. The initWithFormat: message then initializes the contents of the string.

In Java, this would be something like:

public class NSString {
  public static NSString alloc() {
    return new byte[<size of a string>];
  }
  public void initWithParameters(Map<String, String> params) {
    /* initialize */
  }
}

and in some other file:

NSString str = NSString.alloc().initWithParameters(params);

but Java's object model is not quite the same as Objective-C, so this is only an approximation.

如歌彻婉言 2024-12-19 07:16:49

我假设您的困惑在于点表示法,因为这经常让新开发人员感到困惑。这是该语言的一个尴尬且最近添加的内容。我还将假设 mkb 的编辑是正确的,因为您的原始代码不合法​​。

此调用:

[[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"];

与此调用完全相同:

[[[self objname] objectAtIndex:[indexPath row]] objectForKey:@"somename"];

在像 C++ 这样的语言中,它可能是等效的:

this.objname.objectAtIndex(indexPath.row).objectForKey("somename");

这是一个粗略等效,因为 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:

[[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"];

Is precisely the same as this call:

[[[self objname] objectAtIndex:[indexPath row]] objectForKey:@"somename"];

It's rough likely equivalent in a language like C++ would be this:

this.objname.objectAtIndex(indexPath.row).objectForKey("somename");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文