尝试 运行 实例索引中的随机数,但出现索引超出范围错误
Trying to run random number in instance's index but i get index out of range error
我创建了一个结构来连接到 mysql 数据库,然后为它创建了一个实例。当我尝试 运行 一个随机数(在数组的大小内)时,我得到默认数 0,但我的数据库 ID 从 1 开始,因此我得到索引超出范围的错误。
import UIKit
struct PickUpLine: Decodable {
let id: Int
let setup: String
}
class PickUpLines: UIViewController {
private var pul = [PickUpLine ] ()
@IBOutlet var PULViewController: UIView!
@IBOutlet weak var PULabel: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
downloadJson()
}
@IBAction func generateRandPUL(_ sender: Any) {
showRandPUL ()
}
func showRandPUL (){
let randomPUL = Int (arc4random_uniform(UInt32(pul.count)))
PULabel.text = pul[randomPUL].setup // index out of range
}
func downloadJson () {
let url = "https://icebreakerappinc.herokuapp.com/pickuplines"
print(url)
//creating let for the url to bond with the link above #2
guard let url2 = URL (string: url) else {return}
URLSession.shared.dataTask(with: url2) { (data,response,err) in
guard let data = data else {return}
do {
let PULurl = try JSONDecoder().decode([PickUpLine].self, from: data)
DispatchQueue.main.async {
self.PULViewController.reloadInputViews()
self.pul = PULurl
}
}catch let jsonErr {
print("Error serializing json:" , jsonErr)
}
}.resume()
}
}
只需在pul
array
上使用randomElement
,
let randomPUL = pul.randomElement()
puLabel.text = randomPUL?.setup
Call randomElement() to select a random element from an array or
another collection.
Returns
A random element from the collection. If the collection is empty, the
method returns nil.
注意: 使用 camel case
命名变量。它应该是 puLabel
而不是 PULabel
.
更新:
这是 PickUpLines
控制器的样子,
class PickUpLines: UIViewController {
private var pul = [PickUpLine ] ()
func showRandPUL (){
let randomPUL = pul.randomElement()
puLabel.text = randomPUL?.setup
}
//rest of the code...
}
试试这可能是它的工作
guard let count = pul.count - 1 , count >= 0 else { return }
let randomPUL = Int(arc4random_uniform(UInt32(count)))
我创建了一个结构来连接到 mysql 数据库,然后为它创建了一个实例。当我尝试 运行 一个随机数(在数组的大小内)时,我得到默认数 0,但我的数据库 ID 从 1 开始,因此我得到索引超出范围的错误。
import UIKit
struct PickUpLine: Decodable {
let id: Int
let setup: String
}
class PickUpLines: UIViewController {
private var pul = [PickUpLine ] ()
@IBOutlet var PULViewController: UIView!
@IBOutlet weak var PULabel: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
downloadJson()
}
@IBAction func generateRandPUL(_ sender: Any) {
showRandPUL ()
}
func showRandPUL (){
let randomPUL = Int (arc4random_uniform(UInt32(pul.count)))
PULabel.text = pul[randomPUL].setup // index out of range
}
func downloadJson () {
let url = "https://icebreakerappinc.herokuapp.com/pickuplines"
print(url)
//creating let for the url to bond with the link above #2
guard let url2 = URL (string: url) else {return}
URLSession.shared.dataTask(with: url2) { (data,response,err) in
guard let data = data else {return}
do {
let PULurl = try JSONDecoder().decode([PickUpLine].self, from: data)
DispatchQueue.main.async {
self.PULViewController.reloadInputViews()
self.pul = PULurl
}
}catch let jsonErr {
print("Error serializing json:" , jsonErr)
}
}.resume()
}
}
只需在pul
array
上使用randomElement
,
let randomPUL = pul.randomElement()
puLabel.text = randomPUL?.setup
Call randomElement() to select a random element from an array or another collection.
Returns
A random element from the collection. If the collection is empty, the method returns nil.
注意: 使用 camel case
命名变量。它应该是 puLabel
而不是 PULabel
.
更新:
这是 PickUpLines
控制器的样子,
class PickUpLines: UIViewController {
private var pul = [PickUpLine ] ()
func showRandPUL (){
let randomPUL = pul.randomElement()
puLabel.text = randomPUL?.setup
}
//rest of the code...
}
试试这可能是它的工作
guard let count = pul.count - 1 , count >= 0 else { return }
let randomPUL = Int(arc4random_uniform(UInt32(count)))