简体   繁体   English

Swift:如何在一个类中初始化一个类的实例

[英]Swift: How to initialize instance of a class within a class

I am baffled by the errors arising while trying to initialize an instance of an array in a class. 我对尝试在类中初始化数组实例时出现的错误感到困惑。 The comments below are the errors xcode 6 is showing. 下面的注释是xcode 6显示的错误。

I have created a class. 我创建了一个类。 It is having instance of NSMutableArray. 它具有NSMutableArray的实例。 I want to initialize the array (hence calling self.instancename.init()). 我想初始化数组(因此调用self.instancename.init())。 It complains if I don't. 如果我不这样做,它会抱怨。 It complains if I do. 如果我这样做,它会抱怨。

import Foundation 进口基金会

class testclass:NSObject {

    var list_of_things:NSMutableArray;

    init (){  // Designated initializer for 'testclass' cannot delegate (swith self.init);
              // did you means this to be a convenience initializer?
        self.list_of_things.init();
              // 'init' can only refer to the initializers of 'self' or 'super'
        super.init()
              // Initializer cannot both delegate ('self.init') and chain to a superclass
              // initializer ('super.init')
    }
}

You need to assign a value to the variable, there is nothing in that variable to call init on: 您需要为变量分配一个值,该变量中没有任何东西可以调用init

init () {
    self.list_of_things = NSMutableArray()
    super.init()
}

Also a few notes: 还有一些注意事项:

  1. You do not need semicolons at the end of lines (I know that habit is hard to break) 您不需要在行尾使用分号(我知道习惯很难打破)
  2. You do not need to inherit from NSObject 您不需要从NSObject继承
  3. You should prefer to use native swift arrays (Array) instead of NSMutableArray 您应该更喜欢使用本机Swift数组(Array)而不是NSMutableArray
  4. Classes should always start with capital letters 上课应始终以大写字母开头
  5. Variable names should use camel case instead of underscores 变量名称应使用驼峰大小写而不是下划线

This would be my cleaned up version of your test class: 这将是我对测试类的清理版本:

class TestClass {
    var listOfThings: [AnyObject]

    init () {
        self.listOfThings = []
        super.init()
    }
}

And actually, if you just want to initialize to an empty array, you don't even need to implement init or specify the type explicitly: 实际上,如果您只想初始化为空数组,则甚至无需实现init或显式指定类型:

class TestClass {
    var listOfThings = []
}

To call the initializer of another class, you simply call it like this: 要调用另一个类的初始化程序,只需像这样调用它:

self.list_of_things = NSMutableArray()

There's no need to implicitly call the init() function, it's implied when adding the () to the class name. 不需要隐式调用init()函数,这是在将()添加到类名时隐含的。

You could also initialize it when you create your property, like this: 您还可以在创建属性时将其初始化,如下所示:

var list_of_things:NSMutableArray = NSMutableArray()

That's not how you call init on NSMutableArray. 那不是您在NSMutableArray上调用init的方式。

class testclass:NSObject {

    var list_of_things:NSMutableArray

    init (){ 
        self.list_of_things = NSMutableArray()
        super.init()
    }
}

And get rid of those semicolons! 并摆脱那些分号! What is this? 这是什么? last week? 上个星期?

End to end answer. 端到端答案。 No superclass, using Swift arrays, init list_of_things at declaration side, in init() no superclass to initialize. 没有超类,使用Swift数组,在声明端使用init list_of_things,在init()没有要初始化的超类。

class testClass {

  var list_of_things = []

  init () { 
  }
}

126> var abc = testClass ()
abc: testClass = {
  list_of_things = @"0 objects"
}
127> abc.list_of_things
$R55: __NSArrayI = @"0 objects"

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

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