这个语法在Objective

编程入门 行业动态 更新时间:2024-10-15 02:26:32
这个语法在Objective-C中意味着什么?(What does this syntax mean in Objective-C?)

考虑以下:

- (id)initWithTitle:(NSString *)newTitle boxOfficeGross:(NSNumber *)newBoxOfficeGross summary:(NSString *)newSummary;

这是什么意思? 我已经猜到它会返回id,并且需要三个参数,但语法的每个部分是什么意思? 我来自Ruby / JS背景,我觉得这个语法有点难以掌握。

Consider the following:

- (id)initWithTitle:(NSString *)newTitle boxOfficeGross:(NSNumber *)newBoxOfficeGross summary:(NSString *)newSummary;

What does this mean? I've guessed that it returns id, and takes three params, but what does each part of the syntax mean? I come from a Ruby/JS background and am finding this syntax a little hard to grasp.

最满意答案

它是一个名为initWithTitle:boxOfficeGross:summary:的实例方法(即,不是静态或“类”方法) initWithTitle:boxOfficeGross:summary:返回id类型的对象(泛型对象)。 它需要三个参数:String对象,Number对象和另一个String对象。

你这样调用它:

NSNumber * gross = [NSNumber numberWithInteger:1878025999] Movie * avatar = [[Movie alloc] initWithTitle:@"Avatar" boxOfficeGross:gross summary:@"Pocahontas in the 22nd century"]; //or you can do it all on one line, like so: Movie * avatar = [[Movie alloc] initWithTitle:@"Avatar" boxOfficeGross:gross summary:@"Pocahontas in the 22nd century"];

It's an instance method (ie, not a static or "class" method) called initWithTitle:boxOfficeGross:summary: that returns an object of type id (generic object). It takes three parameters: a String object, a Number object, and another String object.

You invoke it like this:

NSNumber * gross = [NSNumber numberWithInteger:1878025999] Movie * avatar = [[Movie alloc] initWithTitle:@"Avatar" boxOfficeGross:gross summary:@"Pocahontas in the 22nd century"]; //or you can do it all on one line, like so: Movie * avatar = [[Movie alloc] initWithTitle:@"Avatar" boxOfficeGross:gross summary:@"Pocahontas in the 22nd century"];

更多推荐

本文发布于:2023-08-06 20:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   Objective

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!