Swift从数组中删除的文档是“删除并返回指定位置的元素”。当我尝试它时,我没有任何值。如果我分两个步骤进行,则效果很好。我想念什么? 主要代码:
func GetItemAlert(s: String){
// Create an alert
let alert = UIAlertController(
title: "New item",
message: s,
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
//alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}
func pop()-> String{
let removeid = Int(arc4random_uniform(UInt32(TheList.count)))
let val1 = TheList[removeid]
let val2 = TheList.remove(at: removeid)
tableView.reloadData()
return ""
}
@IBAction func btnPop(_ sender: Any) {
GetItemAlert(s: pop())
}
扩展名:
import Foundation
class HatTrick
{
var item: String
public init(item: String)
{
self.item = item
}
}
extension HatTrick
{
public class func preload() -> [HatTrick]
{
return [
HatTrick(item: "A"),
HatTrick(item: "B"),
HatTrick(item: "C"),
HatTrick(item: "D"),
HatTrick(item: "E")
]
}
}
答案 0 :(得分:0)
您说的正确的是删除“删除并返回指定位置的元素。”
var a = [0, 1, 2, 3] // Creates an array
let b = a.remove(at: 2) // we removed a value from a and stored it in b
print(b) // Prints 2
答案 1 :(得分:0)
也许在调试器中没有显示val2
导致Xcode优化了它们。您可以尝试用它们调用一个函数,看看它是否仍然优化。
Val1仍在数组内引用,直到在下一次调用中将其删除为止,是否保留?如果您愿意,可以提交错误报告。