我是编程新手。我想在iOS上制作一个简单的游戏。我有一个带有名单列表的数组,我有两个按钮,可以从数组中随机选择项目。
我的问题是,在选择ARRAY ITEM后,我需要添加哪些代码才能删除ARRAY ITEM。
var playerName = ["James", "John", "Paul", "Chris", "BoB", "Sam", "Mike", "Jason"]
//IBOutlets
@IBOutlet weak var player1BTN: UIButton!
@IBOutlet weak var player2BTN: UIButton!
//IBActions
@IBAction func player1BTNpressed(_ sender: Any) {
print("player 1")
//Randomize player from 0 to 26
let player1 = Int(arc4random_uniform(26))
// Change Button Title Text
player1BTN.setTitle((playerName[player1]), for: UIControlState.normal)
}
@IBAction func player2BTNpressed(_ sender: Any) {
print ("player 2")
//Randomize player from 0 to 26
let player2 = Int(arc4random_uniform(26))
// Change Button Title Text
player2BTN.setTitle((playerName[player2]), for: UIControlState.normal)
}
希望这是有道理的。谢谢你的帮助。
答案 0 :(得分:0)
Please try this code, it might be helped you:-
var playerName = ["James", "John", "Paul", "Chris", "BoB", "Sam", "Mike", "Jason"]
let item = "Chris"
if let index = playerName.index(of: item) {
playerName.remove(at: index)
print(playerName)
}
Output like this:- ["James", "John", "Paul", "BoB", "Sam", "Mike", "Jason"]