试图从数组随机元素存储在变量斯威夫特2时出错 [英] Error when trying to store random element from array in variable Swift 2

查看:133
本文介绍了试图从数组随机元素存储在变量斯威夫特2时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我区codeS的数组,我从一个变量数组试图存储一个随机元素,我得到一个错误实例成员区域codeS不能对类型'的ViewController使用'。有什么建议么?

  VAR区域codeS = [209,213,310,323,408,415]
VAR firstThree =区域codeS [INT(arc4random_uniform(UInt32的(区codes.count)))]


解决方案

您无法访问您在初始化时数组。你的属性改为只读属性计算:

  VAR firstThree:诠释{返回区codeS [INT(arc4random_uniform(UInt32的(区codes.count)))]}

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)))]

解决方案

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)))] }

这篇关于试图从数组随机元素存储在变量斯威夫特2时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆