简体   繁体   English

我不了解类的Objective-C初始化程序。

[英]I do not understand Objective-C Initializer for a class.

This code is from the Sams Teach Yourself Swift book, Chapter 21. The Song.h file contains the following code: 此代码摘自Sams Teach Yourself Swift书,第21章。Song.h文件包含以下代码:

//Song.h
#import <Foundation/Foundation.h>
@interface Song : NSObject;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *artist;
@property (nonatomic, strong) NSNumber *rating;
- (instancetype)initWithTitle:(NSString *)title artist:(NSString *)artist rating:(NSNumber *)number;
@end

To my understanding, the - (instancetype) creates an instance method, returning an instancetype . 据我了解, - (instancetype)创建一个实例方法,返回一个instancetype The two (NSString *) s declare that the parameter will be an NSString. 两个(NSString *)声明该参数将为NSString。 I assume that the word title immediately following the first (NSString *) is the parameter name, same with the artist following the (NSString *) . 我假设紧跟在第一个(NSString *)之后的单词title是参数名称,与(NSString *)之后的artist相同。 What is the initWithTitle: and the artist: ? 什么是initWithTitle:artist:

instancetype is a replacement for the id (and in fact the compiler coverts id to instancetype for init methods at compile time) or specific type class usually returned by init methods and can't be used anywhere else. instancetype替代id (实际上,编译器在编译时将id转换为init方法的instancetype )或通常由init方法返回的特定类型类,并且不能在其他任何地方使用。 It allows the compiler to check that assignments from that init method are the appropriate type while still allowing subclasses to inherit initializers. 它允许编译器检查该init方法的赋值是否是适当的类型,同时仍然允许子类继承初始化程序。 So this init method most likely takes the initial values and assigns them to the properties. 因此,此init方法很可能采用初始值并将其分配给属性。

It is the method's signature . 它是方法的签名 In other words ..this is how the method is named . 换句话说,..方法就是这样命名的


Btw. 顺便说一句。 "instancetype" is a compiler keyword, that represents the instance's type, so actually the method will return a Song instance. “ instancetype”是一个编译器关键字,代表实例的类型,因此实际上该方法将返回Song实例。

- initWithTitle:artist:rating: is the method's name, or in Objective-C terms, selector. - initWithTitle:artist:rating:是方法的名称,或者用Objective-C的术语来说是选择器。 title , artist and rating are parameter names. titleartistrating是参数名称。

As for - (instancetype) , the minus sign - means it's an instance method (class methods begin with a plus sign + ), while instancetype is a return type of a method. 至于- (instancetype) ,减号-表示它是一个实例方法(类方法以加号+开头),而instancetype是方法的返回类型。 It's just a keyword that tells the compiler this method will always return an object of type of class on which it was called. 它只是一个关键字,告诉编译器此方法将始终返回调用它的类类型的对象。 If you want to know more about it, I suggest reading the NSHipster post about instancetype . 如果您想了解更多信息,我建议阅读有关instancetypeNSHipster帖子

You said: 你说:

To my understanding, the - (instancetype) creates an instance method, returning an instancetype . 据我了解, - (instancetype)创建一个实例方法,返回一个instancetype

The - designates an instance method. -指定实例方法。 The instancetype is a special keyword designating that it returns an instance of the class, a Song instance in this case. instancetype是一个特殊的关键字,它指定它返回类的实例,在这种情况下为Song实例。

The two (NSString *) s declare that the parameter will be an NSString . 这两个(NSString *)声明该参数将为NSString

Yes, the two (NSString *) references indicate that the first two parameters are string values. 是的,两个(NSString *)引用指示前两个参数是字符串值。 The following (NSNumber *) indicates that the third parameter is a number object. 以下(NSNumber *)指示第三个参数是数字对象。

I assume that the word title immediately following the first (NSString *) is the parameter name, same with the artist following the (NSString *) . 我假设紧跟在第一个(NSString *)之后的单词title是参数名称,与(NSString *)之后的艺术家相同。 What is the initWithTitle: and the artist: ? 什么是initWithTitle:artist:

The word immediately following the (NSString *) is the name of the parameter used within the implementation of that method. (NSString *)之后的单词是该方法的实现中使用的参数名称。

Consider the declaration: 考虑一下声明:

- (instancetype)initWithTitle:(NSString *)title artist:(NSString *)artist rating:(NSNumber *)number;

This declares a method called initWithTitle:artist:rating: , that takes three parameters, a title, artist, and rating. 这将声明一个名为initWithTitle:artist:rating: ,该方法带有三个参数:标题,艺术家和等级。 So if you wanted to create a song called "Ticket to Ride" by the "Beatles" and a rating of 5, you would call this method with something like: 因此,如果您想通过“甲壳虫”创建一首名为“ Ticket to Ride”的歌曲,并将其等级定为5,则可以使用以下方法调用此方法:

Song *song = [[Song alloc] initWithTitle:@"Ticket to Ride" artist:@"Beatles" rating:@5];

You pretty much have it correct. 您几乎正确了。

An Obj-C instance method begins with a hyphen, "-" followed by the return type in parentheses. Obj-C实例方法以连字符“-”开头,后跟括号中的返回类型。 A Class method, begins with a plus "+" but is otherwise the same. Class方法以加号“ +”开头,但其他方面相同。

That's followed by the rest of the method signature, which can include multiple parameters. 接下来是方法签名的其余部分,其中可以包括多个参数。 Each parameter is preceded by a colon ":", then the required type for the argument/parameter in parentheses, eg NSString * , which is followed finally an internal name for the value that will be passed in. You read the entire method name by stating each parameter... 每个参数都以冒号“:”开头,然后是括号中的参数/参数的必需类型,例如NSString * ,最后是将要传入的值的内部名称。说明每个参数...

initWithTitle:artist:rating

Read as a sentence, you're saying: 阅读作为一句话,你说的是:

"Inititialize a Song instance with a title (that's an NSString*), an artist (also an NSString*), and a rating (this requires an NSNumber *)" “使用标题(是NSString *),艺术家(也是NSString *)和评分(这需要NSNumber *)来初始化Song实例”

-(returnType)firstPartofMethodNameThatTakestheArgument:(NSString *)argument1 andArgumentTwo:(NSString *)argument2

The instanceType is a relatively new alternative for id which meant the return type could effectively be anything. instanceTypeid一个相对较新的替代方法,这意味着返回类型可以有效地是任何值。 This instead ensures that it can only be an instance of the type containing the method, in this case a Song . 而是确保它只能是包含该方法的类型的实例,在本例中为Song

As for the duplicate argument names... The first part " artist: is the external name that appears when you call the method (if you use code completion for example, that's what shows up). The part after the argument's type (NSString *)artist is the internal name that will be used inside the method implementation. Those can be the same, but they don't have to be. 至于重复的参数名称...第一部分“ artist:是调用该方法时出现的外部名称(例如,如果使用代码完成,则显示该名称)。参数类型之后的部分(NSString *)artist是将在方法实现中使用的内部名称。这些名称可以相同,但不必相同。

Swift has a similar construction, but with more options where you can choose to write different internal and external parameter names, or just use 1 (or use in-out parameters whose values chance, use variadic parameters that can be an arbitrary number of elements, etc - look into Swift Functions for more details on those topics). Swift具有类似的构造,但是具有更多选项,您可以选择编写不同的内部和外部参数名称,或者仅使用1(或使用值可能会出现的in-out参数,使用可以是任意数量元素的可变参数,等等-有关这些主题的更多详细信息,请查看Swift 函数

func initWith(externalTitle internalTitle:String, #artist:String, #rating:Int) -> Song 

//When calling, you'd see "externalTitle:artist:rating" 
//but in the method implementation you'd use "internalTitle" instead.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM