2

I created a multidimensional array, but when I try to access it in viewDidLoad the application crashes. Any suggestions as to why this is happening?

I also tried to define my array as var data: KeyLabelType[][], but that didn't help either.

class MyViewController: UIViewController {

    var data: Array<Array<KeyLabelType>> = []

    init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!)  {

        let array1: KeyLabelType[] = [...]
        let array2: KeyLabelType[] = [...]
        let array3: KeyLabelType[] = [...]

        self.data = [array1, array2, array3]

        super.init(nibName: nibNameOrNil, bundle: nibBundle)
    }

    override func viewDidLoad()  {

        let count = self.data.count // Application crashes here
    }
}

enter image description here

EDIT:

I declared KeyLabelType as

enum KeyLabelType: Character {
    case a = "a"
    case b = "b"
    case c = "c"
}

EDIT: I filed a bug report with Apple (17340589)

5
  • How is your KeyLabelType class declared? I just tested your code in both playground and an app and it is working. I declared it as class KeyLabelType {}. Commented Jun 16, 2014 at 12:45
  • I declared KeyLabelType as an enum. I there something I'm missing? Commented Jun 16, 2014 at 13:24
  • There is nothing wrong with your code. This is just a guess, but might it be that you are running this code too early and that it all needs to happen in or after viewDidLoad? Commented Jun 16, 2014 at 15:29
  • @GoodbyeStackOverflow I also tried putting everything in viewDidLoad, but I'm still getting the same error. I set self.data=[] in the initializer and then populated data in viewDidLoad. Commented Jun 16, 2014 at 16:04
  • If you place your code in a separate class and then create an instance of that class in viewDidLoad it runs fine. In addition, if you create an Int array that works as expected inside the ViewController code following your current process. I can only think therefore that it doesn't work because of the complexity of the array or the enum creation. Commented Jun 16, 2014 at 16:41

1 Answer 1

4

This is a bug, please report it at bugreport.apple.com. It is some how related to subclassing NSObject in swift.

import Foundation

enum KeyLabelType: Character {
    case a = "a"
    case b = "b"
    case c = "c"
}

class MyClass: NSObject /* Removing NSObject here will fix the crash */ {
    var myData: Array<Array<KeyLabelType>>

    init() {
        let array1: Array<KeyLabelType> = [.a, .b]
        let array2: Array<KeyLabelType> = [.b]
        let array3: Array<KeyLabelType> = [.c]

        self.myData = [array1, array2, array3]
    }
}

var myObject = MyClass()
myObject.myData //Crash here, if subclassing NSObject. No crash if NSObject is removed

If you could rewrite your code to use NSObject subclasses all the way it might also work as a workaround

import Foundation

class KeyLabelType: NSObject {
    let value: Int

    init(_ value: Int) {
        self.value = value
    }

}

class MyClass: NSObject {
    var myData: NSArray

    init() {
        var item = KeyLabelType(1)
        var array1: NSArray = [KeyLabelType(1), KeyLabelType(2)]
        var array2: NSArray = [KeyLabelType(1), KeyLabelType(2)]
        self.myData = [array1, array2]
    }
}

var myObject = MyClass()
myObject.myData.count
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't help you of course as you can't remove the dependency on UIViewController
Thank you. I will report it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.