2

The code being:

class Singleton {

    class var sharedInstance:Singleton {
        struct Static {
            static var instance:Singleton? = nil
            static var token:dispatch_once_t = 0
        }
        dispatch_once(&Static.token)
            {
                Static.instance = Singleton ()
        }
        return Static.instance!
    }

    var prayerArray = Array<PrayerSound>()

}

and:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let prayer = PrayerSound(namazName: cellLabel, pathString: filePath!, checked: checked)
        sinleton.prayerArray[cellindex] = prayer

}
3
  • what is in your –numberOfSectionsInTableView: and –tableView:numberOfRowsInSection: methods' bodies? Commented Oct 2, 2015 at 7:59
  • the dispatch_once part is not needed at all. This singleton syntax uses dispatch_once automatically behind the scenes. Commented Oct 2, 2015 at 8:06
  • You are correct, but the error has nothing to do with the singleton - he's got an empty array. Commented Oct 2, 2015 at 8:10

1 Answer 1

1

You are initialising prayerArray as an empty array with the line var prayerArray = Array<PrayerSound>(). Therefore it has no elements. You have to either initialise it to have a certain length, or append items to it. I think what you want is to do is to initialise it to have enough elements to accept your indices. Something like

var prayerArray = Array<PrayerSound>(count:64, repeatedValue: somePrayer)

Alternatively, you can declare it as an array of optionals, and set them to nil:

var prayerArray = Array<PrayerSound?>(count:64, repeatedValue: nil)

Sign up to request clarification or add additional context in comments.

Comments

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.