我正在尝试随机化arrayList,然后将URL拉到IBAction,然后打开页面。
override func viewDidLoad() {
let ArrayList = [
["A", "A2", "http://a.com"],
["B", "B2", "http://b.com"],
["C", "C2", "http://c.com"],
]
let pickArrayList = ArrayList[Int(arc4random_uniform(UInt32(ArrayList.count)))]
label1.text = pickArrayList[0]
label2.text = pickArrayList[1]
}
下面是IBAction但我收到一条错误,说pickArrayList [2]是一个未解析的标识符。
@IBAction func didTapButton(sender: AnyObject) {
UIApplication.shared.open(URL(pickArrayList[2])!)
}
提前致谢, Swift Noobie
答案 0 :(得分:1)
发生错误是因为pickArrayList
在viewDidLoad
范围内被声明为局部变量。
将pickArrayList
声明为类顶层的属性
var pickArrayList : [String]!
override func viewDidLoad() {
super.viewDidLoad()
let arrayList = [
["A", "A2", "http://a.com"],
["B", "B2", "http://b.com"],
["C", "C2", "http://c.com"],
]
pickArrayList = arrayList[Int(arc4random_uniform(UInt32(arrayList.count)))]
label1.text = pickArrayList[0]
label2.text = pickArrayList[1]
}
请遵守变量名称以小写字母开头的命名约定。