1

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')
    }
}
Chetan Purohit
  • 364
  • 1
  • 3
  • 16
David Johnston
  • 976
  • 1
  • 8
  • 10

4 Answers4

4

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 = []
}
drewag
  • 93,393
  • 28
  • 139
  • 128
  • Thank you. That answers my question and a number of other questions that were in my head. I agree that the semicolons is just a habit picked up from C and systemVerilog. I should switch my brain to Python mode where I don't use semicolons. – David Johnston Jun 07 '14 at 20:37
  • Except for this one: "Variable names should use camel case instead of underscores" - I've been used underscore in C, and then in C++, and then in Obj-C (unless initWithXXX due to objc runtime). And I'll keep using that in SWIFT. – superarts.org Sep 15 '14 at 01:24
  • 1
    @superarts.org, you are of course entitled to use underscores if you prefer it, but you will have a harder time collaborating with most other developers because the vast majority will be using camel case. Not following conventions is a barrier to communicating with others. – drewag Sep 15 '14 at 01:33
  • Array is Struct while NSMutableArray is class you cannot achieve some functionalities interchangeably – Asad Mehmood Jan 01 '21 at 06:18
2

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()
Simon Germain
  • 6,834
  • 1
  • 27
  • 42
0

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?

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
0

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"
GoZoner
  • 67,920
  • 20
  • 95
  • 145