ios初学者,希望大侠能给我理论指导一下!

解决方案 »

  1.   

    alloc方法只用来分配内存,而init是对对象进行初始化方法,两个方法合起来等于Java的new或OC的new。如果不init的话,内存中还有之前的数据
      

  2.   

    你应该是对面向对象编程不了解。 在oc中基本上所有的类都是继承自NSObject 而来,而继承是面向对象编程中一个很重要的特性。
    子类继承父类,并获得父类相关的属性和方法,所以在子类初始化时必须首先调用父类的初始化方法,以实现父类相关资源的初始化。
      

  3.   

    对三楼做点补充 你不调用[super init]那就不做这一行啊,简单的说就是这个子类对父类有继承属性,方法,但是不做父类初始化,(一般我们会在父类里对property做些初始化,或者调用一些方法什么的,但是你错过了这句,这些动作完全跳过)
      

  4.   

    We just said that init methods like this one can return totally different objects. Remember
    that instance variables are found at a memory location that’s a fixed distance from the hidden
    self parameter. If a new object is returned from an init method, we need to update
    self so that any subsequent instance variable references affect the right places in memory.
    That’s why we need the self = [super init] assignment. Keep in mind that this assignment
    affects the value of self only for this method. It doesn’t change anything outside the
    method’s scope.