ios - swift新手请教,我在swift中调用self.init,报错了,怎么回事?
伊谢尔伦
伊谢尔伦 2017-04-18 09:47:55
0
2
470

在方法中药调用 self = self.init,报错信息是:Cannot assign to value:'self'is immutable
代码如下:

class Node:NSObject{


var parentID:Int?//父节点的id,如果为-1表示该节点为根节点

var nodeID:Int?//本节点的id

var name:NSString?//本节点的名称

var depth:Int?//本节点的深度

var expand:Bool?//该节点是否处于展开状态

//快速实例化该对象模型

func initWithParentID(parentID:Int,nodeID:Int,name:NSString,depth:Int,expand:Bool) -> Self{
    
    self = self.init
    
    return self
    
}

}

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

répondre à tous(2)
小葫芦

Le message d'erreur n'est-il pas très clair ? Self ne peut pas être modifié - -
Appelez d'abord la fonction d'initialisation de la classe parent

  init(parentID: Int, nodeID: Int, name: NSString, depth: Int, expand:Bool) {
    super.init()
    self.parentID = parentID
    self.nodeID = nodeID
    self.name = name
    self.depth = depth
    self.expand = expand
  }
左手右手慢动作

Raison fondamentale :
La chaîne d'initialisation de Swift est différente de celle d'OC. OC hérite uniformément de NSObject, donc super.init dans OC est la classe parent d'initialisation.
Mais Swift s'initialise d'abord, puis remonte la chaîne d'héritage pour initialiser les propriétés héritées de la classe parent. Après avoir atteint la classe de base, il termine ensuite l'opération d'initialisation vers le bas.

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!