0

I have an array of area codes and I am trying store a random element from the array in a variable and I get an error "instance member 'areaCodes' can not be used on type 'ViewController' ". Any suggestions?

var areaCodes = [209, 213, 310, 323, 408, 415]
var firstThree = areaCodes[Int(arc4random_uniform(UInt32(areaCodes.count)))]
3
  • Please show where/how you're doing this in your view controller. There is currently not enough information to understand what it wrong. Sidenote: Your code works in a playground Commented Jan 29, 2016 at 4:36
  • Those two lines work fine on swiftstub, so more context is needed to understand your problem. Commented Jan 29, 2016 at 4:37
  • It crashes always or sometimes? Commented Jan 29, 2016 at 5:06

2 Answers 2

1

You can not access your array at initialization time. Change your property to a read only computed property:

var firstThree: Int { return areaCodes[Int(arc4random_uniform(UInt32(areaCodes.count)))] }
Sign up to request clarification or add additional context in comments.

Comments

0

You have to assign the firstThree variable inside a function of your class.

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.