1

In TerningspilletSpillereViewController I have an array that looks like this:

var mineSpillere = ["1SP", "2SP", "3SP"]

and this under:

func assignArray() {
   let obj = TerningspilletViewController()
   obj.mineSpillere2 = self.mineSpillere
}

In my other UIViewController (TerningspilletViewController) I have this

var mineSpillere2 = [String]()

I want to generate a random (1SP, 2SP or 3SP) on button click like this from a function:

func generateNumber() {
    let array = aBB
    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))

    randomLabel.text = (array[randomIndex])
}

But this gives me the following error:

fatal error: Array index out of range.

I this line randomLabel.text = (array[randomIndex])

Why is that?

10
  • What kind of error do you get? What is the exact error message? Commented Jul 22, 2015 at 21:03
  • @JimiLoe - Image of error added in question. Commented Jul 22, 2015 at 21:09
  • change array.count to array.count - 1. The count is the total number of items in an array but array's are accessed on a 0 based index, so a count of 3 really means you can access index 0, 1, or 2. Commented Jul 22, 2015 at 21:18
  • @thefredelement - Still does not work, error: s18.postimg.org/w9mb6o655/… Commented Jul 22, 2015 at 21:22
  • I don't understand the relationship between your two aBB arrays, or the other view controller. if you're re-declaring aBB and not asking for the one with values in your generateNumber function this will not work because array.count is 0. Commented Jul 22, 2015 at 21:30

2 Answers 2

2

See this example and update for your usage. I'm not quite understanding your naming but this is how you can get the data from a different view controller (that is initialized with the data).

First View Controller:

import UIKit

class ViewController: UIViewController {

var arrayWithoutData = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    assignArray()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func assignArray() {

    let otherVC = NextViewController()
    arrayWithoutData = otherVC.mineSpillere

    println(arrayWithoutData)

}

}

Second View Controller:

import UIKit

class NextViewController: UIViewController {

var mineSpillere = ["1SP","2SP","3SP"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

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

2 Comments

Thanks, it worked!!!! Do i need the "prepareForSegue"? What is that for in this case?
In your case you do not, You are only creating an instance of a view controller and then getting a value from one of it's properties (the array you created). If you want to move data to different view controllers that a user is navigating between then you can use prepare for segue to set properties to other view controllers. You can use NSNotificationCenter to send data to already loaded objects as well.
0

That error could occur for two reasons:

  • The array length changes between the call to array.count and the array access. This could only happen if you assign or modify the array from another thread
  • The array is empty, in which case arc4random_uniform can never produce a number 0<=X<0 (and whatever it returns will trigger an index-out-of-bounds exception).

So if you are not using multiple threads, check if the array is empty.

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.