David Johnston
David Johnston

Reputation: 1026

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.

I have created a class. It is having instance of NSMutableArray. I want to initialize the array (hence calling 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')
    }
}

Upvotes: 1

Views: 12683

Answers (4)

drewag
drewag

Reputation: 94683

You need to assign a value to the variable, there is nothing in that variable to call init on:

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
  3. You should prefer to use native swift arrays (Array) instead of 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:

class TestClass {
    var listOfThings = []
}

Upvotes: 4

GoZoner
GoZoner

Reputation: 70135

End to end answer. No superclass, using Swift arrays, init list_of_things at declaration side, in init() no superclass to initialize.

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"

Upvotes: 0

Connor
Connor

Reputation: 64634

That's not how you call init on NSMutableArray.

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?

Upvotes: 0

Simon Germain
Simon Germain

Reputation: 6844

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.

You could also initialize it when you create your property, like this:

var list_of_things:NSMutableArray = NSMutableArray()

Upvotes: 2

Related Questions