简体   繁体   English

一旦删除项目后如何重新填充数组

[英]how to repopulate an array once items have been removed swift

I have an array and remove items so that they do not get repeated while calling elements from the array. 我有一个数组,并删除了一些项,以便它们在从数组中调用元素时不会重复出现。 However, once all elements are called I want to repopulate the array after OK is clicked on the alert. 但是,一旦调用了所有元素,我想在警报上单击“确定”后重新填充数组。 I can not figure out how to do this. 我不知道该怎么做。 Any ideas? 有任何想法吗?

func select() {
    //random phrase
    if array.count > 0 {
        let Array = Int(arc4random_uniform(UInt32(array.count)))
        let randNum = array[Array]
        // random phrase when program starts
        self.phrase.text = (array[Array])
        //removing
        array.remove(at: Array)
        array.
    } else {
        let  alert = UIAlertController(title: "Completed", message: "Click below to reload datac", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "ok", style: .default, handler: nil))
        present(alert, animated: true)
    }
}

General psuedo code would be: 一般伪代码为:

declare an array with items
invoke select() to choose a random item
    if array is empty 
        re-populate array after user prompt
        return
    end-if

    select random item and assign to phrase 
    remove item from array 
end select()

So something to the effect of: 所以有以下效果:

var items = ["a", "b", "c"]
var phrase: String?

func selectRandomItem() {
    if items.isEmpty {
        let  alert = UIAlertController(title: "Completed", message: "Click below to reload datac", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "ok", style: .default, handler: { _ in
            // repopulate `items` array
            items = ["a", "b", "c"]
        }))
        present(alert, animated: true)
        return
    }

    let index = Int(arc4random_uniform(UInt32(items.count)))
    phrase = items[index]
    items.remove(at: index)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何跟踪数组中的哪些项已经存在? - how to keep track of what items in an array have already been? 在 swift 4.0 中将值附加到数组后如何重新加载 tableView - How to reload a tableView once a value has been appended to an array in swift 4.0 Swift 3中已删除如何修复C样式的语句? - How to fix C-style for statement has been removed in Swift 3? 如何快速修剪数组项目? - How to trim Array items in swift? indexsOfObjectsPassingTest:| 找到索引后,如何检查它们是否为顺序索引? - indexesOfObjectsPassingTest: | once indexes have been found how to check if if they are sequential? 设置两个变量后如何调用方法 - How to call a method once two variables have been set 如何更新以前在代码中设置的NSLayoutConstraint常量? -迅捷 - How to update NSLayoutConstraint constants, that have previously been set in code? - Swift 如何在Swift 4中向数组添加或添加多个项目 - How to append or add multiple items to an array in Swift 4 iOS TableView层次结构使用NSArray和ARC,如何重新填充数组? - iOS TableView Hierarchy using NSArray with ARC, how to repopulate array? C-style for 语句已在 Swift 3 中删除(连续错误) - C-style for statement has been removed in Swift 3 (Continuos error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM