简体   繁体   English

谁能给我一个关于“ self = super init”的简单解释?

[英]Can anyone give me an easy explanation about “self=super init”?

I still don't understand self=super init; 我仍然不了解self = super init; ,although I read many many books and googles. ,尽管我读了很多书和谷歌。 I know that it is used for checking the current object equals to the return object from the parent class etc. However, I can not accept that is the answer. 我知道它用于检查当前对象是否等于父类等的返回对象。但是,我不能接受这就是答案。 Can someone give me an easy but solid acceptable answer? 有人可以给我一个简单但可靠的答案吗? Thanks! 谢谢!

The quick answer is that you are telling the super class of your class to initialise the object. 快速的答案是,您正在告诉您的类的超类初始化该对象。 Because you are inheriting from that super class, then you can perform other initialisation tasks specific to your implementation, but first you have to make sure the basics of initialisation are performed (which is handled by the base class NSObject). 因为您是从该超类继承的,所以您可以执行特定于实现的其他初始化任务,但是首先必须确保执行了初始化的基础(由基类NSObject处理)。 Each class that extends (inherits) from another class has to make sure their super class has been initialised before they can do their initialisation. 从另一个类扩展(继承)的每个类都必须先确保其父类已被初始化,然后才能进行初始化。

A very quickly made up (and therefore probably quite poor) analogue - before you could be created, your parents had to be created (the super class) and they had to have their parents created first (their super class) 一个非常快速组成的(因此可能很差)的类似物-在创建您的父母之前,必须先创建您的父母(超类),并且他们必须首先创建父母(他们的超类)

I suggest you search for object oriented programming basics to learn about this sort of thing. 我建议您搜索面向对象的编程基础知识,以了解这种情况。

Your mention of "checking" makes me think that you are seeing = as a comparison operation. 您提到“检查”使我认为您将=视为比较操作。 It's not. 不是。 It's an assignment. 这是一项任务。 Sometimes the assignment appears within an if condition. 有时,分配出现在if条件中。 That's taking advantage of the fact that assignment expressions have a value -- the value that was assigned. 这利用了赋值表达式具有一个值的事实-被赋值。 So, if (self = [super init]) combines "call super's -init method", "assign the result to self ", and "check if we got non-nil from super's -init " (that is, that super didn't fail to initialize). 因此, if (self = [super init])组合了“调用super的-init方法”,“将结果分配给self “检查我们是否从super的-init得到非零”(也就是说,那个super没有?无法初始化)。

Because this combined assignment-and-test can be confusing and error-prone, Apple now recommends separating the assignment out from the check for failure: 由于这种组合的分配和测试可能会造成混淆并且容易出错,因此Apple现在建议将分配与检查失败分开:

self = [super init];
if (self)
    // ... continue initializing ...

NSObject is root class. NSObject是根类。 https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html You can find an init method there. https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html您可以在此处找到init方法。 That init method within NSObject class is the initializer for every object in Objective-C. NSObject类中的init方法是Objective-C中每个对象的初始化程序。 super simply returns super class of a object. super仅返回对象的超类。

If Class B was inherited from Class A , Class A will be super class of Class B . 如果Class B是从Class A Class A继承的,则Class A Class A将是Class B类的超类。 Again another class Class C which was inherited from Class B , the super class of Class C will be Class B . 再次另一类Class C ,将其从继承Class B ,超类的Class C将是Class B So on. 等等。

When you call [super init]; 当您调用[super init]; from a class, it simply call init method of its super class, which in turn call init method of its super class and so on. 从一个类中,它仅调用init类的init方法,而后者又调用init类的init方法,依此类推。 At last it will reach root object NSObject, and call the real init method. 最后,它将到达根对象NSObject,并调用实际的init方法。 At that point the iOS will allocate memory for your object and necessary data structures for your object. 到那时,iOS将为您的对象分配内存,并为您的对象分配必要的数据结构。 Read documentation about each method and property in NSObject class. 阅读有关NSObject类中每个方法和属性的文档。

If you created a class MyView which is subclass of UIView , your class hierarchy will be. 如果创建了MyView类,它是UIView子类,则您的类层次结构将是。

     NSObject

UIAppearanceContainer

   UIAppearance

     NSCoding

      UIView

      MyView

So when you call init method in your class 'MyView', it will call 因此,当您在类“ MyView”中调用init方法时,它将调用

init method of UIView UIView的init方法

UIView will call init method of NSCoding UIView将调用NSCoding的init方法

NSCoding will call init method of UIAppearance NSCoding将调用UIAppearance的init方法

UIAppearance will call init method of UIAppearanceContainer UIAppearance将调用UIAppearanceContainer的init方法

UIAppearanceContainer will call init method of NSObject. UIAppearanceContainer将调用NSObject的init方法。

暂无
暂无

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

相关问题 if(self = [super init])与if((self = [super init])) - if (self = [super init]) vs. if ((self = [super init])) iPhone - 当[super init]失败时使用self = [super init] - iPhone - Use of self = [super init] when [super init] fails 有人可以帮我了解一下iPhone应用程序中的堆栈跟踪吗? - Can somebody give me a hand about this stacktrace in iPhone app? if(self = [super init]) - LLVM警告! 你是怎么处理它的? - if(self = [super init]) - LLVM warning! How are you dealing with it? 当我初始化自定义单元格时,它没有设置为'[(super或self)init ...]'的结果时返回'self' - Returning 'self' while it is not set to the result of '[(super or self) init…]' when I initialize custom cell 在我的自定义alloc方法中未将其设置为'[((super或self)init…])的结果时返回'self' - Returning 'self' while it is not set to the result of '[(super or self) init…]' in my custom alloc method “self”时使用的实例变量未通过Analyzer设置为“[(super或self)init ...]”的结果 - Instance variable used while 'self' is not set to the result of '[(super or self) init…]' via Analyzer 我需要一些有关DOCX格式同步(iOS)的建议,以使用Google Drive API。 有人可以帮助我吗? - I need some advice about DOCX format sync(iOS) for using google drive api. Anyone can help me? InitWithCoder,[super init]还是[super initWithCoder]? - InitWithCoder, [super init] or [super initWithCoder]? 谁能帮我解决这个xcode问题? - Can anyone help me with this xcode issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM