ios - Error when trying to store random element from array in variable Swift 2 -


i have array of area codes , trying store random element array in variable , error "instance member 'areacodes' can not used on type 'viewcontroller' ". suggestions?

var areacodes = [209, 213, 310, 323, 408, 415] var firstthree = areacodes[int(arc4random_uniform(uint32(areacodes.count)))] 

you can not access array @ initialization time. change property read computed property:

var firstthree: int { return areacodes[int(arc4random_uniform(uint32(areacodes.count)))] } 

Comments