如何在Swift中随机化或混洗数组中的元素?例如,如果我的阵列由52张扑克牌,我想洗牌的阵列,以洗牌.
我在swift 3.0中写作
我有这个代码,它给我警告结果,呼叫未使用
public override init(){
super.init()
}
public init(annotations: [MKAnnotation]){
super.init()
addAnnotations(annotations: annotations)
}
public func setAnnotations(annotations:[MKAnnotation]){
tree = nil
addAnnotations(annotations: annotations)
}
public func addAnnotations(annotations:[MKAnnotation]){
if tree == nil {
tree = AKQuadTree()
}
lock.lock()
for annotation in annotations {
// The warning occurs at this line
tree!.insertAnnotation(annotation: annotation)
}
lock.unlock()
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试在另一个类中使用此方法,但它仍然给我错误,insertAnnotation的代码在上面
func insertAnnotation(annotation:MKAnnotation) -> Bool {
return insertAnnotation(annotation: annotation, toNode:rootNode!)
}
func insertAnnotation(annotation:MKAnnotation, toNode node:AKQuadTreeNode) -> Bool {
if !AKQuadTreeNode.AKBoundingBoxContainsCoordinate(box: node.boundingBox!, coordinate: annotation.coordinate) {
return false …Run Code Online (Sandbox Code Playgroud) 如何将以下功能转换为swift 3?目前收到Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance'错误.
extension MutableCollection where Index == Int {
/// Shuffle the elements of `self` in-place.
mutating func shuffleInPlace() {
// empty and single-element collections don't shuffle
if count < 2 { return }
for i in 0..<count - 1 { //error takes place here
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
} …Run Code Online (Sandbox Code Playgroud) 我有这个扩展,它将创建一个新的数组,其中包含从给定数组中随机组的数组:
extension Array {
var shuffle:[Element] {
var elements = self
for index in 0..<elements.count {
swap(&elements[index], &elements[ Int(arc4random_uniform(UInt32(elements.count-index)))+index ])
}
return elements
}
func groupOf(n:Int)-> [[Element]] {
var result:[[Element]]=[]
for i in 0...(count/n)-1 {
var tempArray:[Element] = []
for index in 0...n-1 {
tempArray.append(self[index+(i*n)])
}
result.append(tempArray)
}
return result
}
}
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
let mainArr = Array(1...60)
let suffeldArr = mainArr.shuffle.groupOf(10)
print(suffeldArr)
Run Code Online (Sandbox Code Playgroud)
它将打印如下:
[[10 random element between 1 to 60], [10 random element between 1 to 60], [10 random element …Run Code Online (Sandbox Code Playgroud) 我想在Swift中生成多个不同的随机数.这是程序.
检查阵列是否为空
a.如果数组为空,请插入随机数
b.如果数组不为空,则将随机数与数组
i中的数字进行比较.如果数字相同,则重复2
ii.如果数字不相同,请插入随机数并重复2
import UIKit
//the random number generator
func randomInt(min: Int, max:Int) -> Int {
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
var temp = [Int]()
for var i = 0; i<4; i++ {
var randomNumber = randomInt(1, 5)
if temp.isEmpty{
temp.append(randomNumber)
} else {
//I don't know how to continue...
}
}
Run Code Online (Sandbox Code Playgroud)到目前为止,我已经创建了一个图像文件夹,并且可以将其中 1 个图像分配给UIImageView.
但我现在想做的是创建一个随机生成器,它将从文件夹中选择一个随机图像,所以如果我要创建一个按钮,我可以在一个UIImageView.
但是,该文件夹将包含 100 多个图像,因此我不想将每个图像硬编码到数组中。此外,图像的名称对于图像本身来说必须是唯一的。
我一直在网上搜索,但找不到有关如何编写适合我的需求的代码的信息。那么我如何快速地从文件夹中获取并显示随机图像?
我想创建随机数,以便在应用程序正面的5个UIImage框中显示.但是,我希望这些数字并不总是在同一范围内.例如,我下面的代码显示0 ... 9 randomPoolBallIndex3.但相反,我希望它显示0到49之间的任何数字,但不会在另一个randomPoolBallIndex上重复相同的数字.所以每次按下按钮都不会显示,比如1,1,34,35和50,但每个数字都会不同.
有没有办法解决这个问题?
我为每个randomPoolBallIndex打破了数组从0到49,但现在他们只会显示我设置范围的内容并且我并不完全满意,同时它解决了重复问题.
代码如下:
let ballArray = ["poolball1","poolball2","poolball3","poolball4","poolball5","poolball6","poolball7","poolball8","poolball9","poolball10","poolball11","poolball12","poolball13","poolball14","poolball15","poolball16","poolball17","poolball18","poolball19","poolball20","poolball21","poolball22","poolball23","poolball24","poolball25","poolball26","poolball27","poolball28","poolball29","poolball30","poolball31","poolball32","poolball33","poolball34","poolball35","poolball36","poolball37","poolball38","poolball39","poolball40","poolball41","poolball42","poolball43","poolball44","poolball45","poolball46","poolball47","poolball48","poolball49","poolball50"]
var randomPoolBallIndex: Int = 0
var randomPoolBallIndex1: Int = 0
var randomPoolBallIndex2: Int = 0
var randomPoolBallIndex3: Int = 0
var randomPoolBallIndex4: Int = 0
var randomPoolBallIndex5: Int = 0
@IBOutlet weak var poolBallView1: UIImageView!
@IBOutlet weak var poolBallView2: UIImageView!
@IBOutlet weak var poolBallView3: UIImageView!
@IBOutlet weak var poolBallView4: UIImageView!
@IBOutlet weak var poolBallView5: UIImageView!
@IBAction func buttonPressed(_ sender: UIButton) {
randomPoolBallIndex1 = Int.random(in: 20 ... 29)
randomPoolBallIndex2 …Run Code Online (Sandbox Code Playgroud) 我想按给定大小的块分割字符串 2
示例:
字符串"1234567"和输出应该是["12", "34", "56","7"]