arc4random_uniform()
메서드를 사용하여 배열에서 요소를 가져올 때 배열과 동일한 요소가 두 번 이상 표시되는 경우가 많습니다. 배열에있는 각 요소를 한 번만 가져올 수 있도록 노력하고 있습니다. 내가 이것을하려고하는 이유는 하나의 셀이 같은 텍스트를 가지고 있지 않기 때문이다.동일한 요소를 두 번 이상 잡지 않고 배열에서 무작위 요소를 잡는 방법?
viewDidLoad()
방법에서
var definitions = ["Used to carry the pharoah","Used to carry bodies as a ceremony","Had a flat deck to carry a farmer's treasure","Daily, it made a trip around the world to carry Ra","Towed by smaller boats, carrying heavy objects","Used for business and pleasure by officials/nobles","Carried most Egyptians and some goods"]
, 내가 definitions
의 임의 요소를 호출하는 이런 짓을했는지 : 내가 너무 내 코드를 변경할 수있는 방법
self.boats = [Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))])]
여기 UITableView
의 세포의 텍스트의 배열이다 두 요소가 내 UITableView
에 두 번 표시되지 않습니까? 고맙습니다!
배열을 무작위로 정렬 하시겠습니까? –
찾고있는 것은 "랜덤 셔플 (random shuffle)"또는 "임의 순열 (random permutation)"입니다. [Fisher-Yates shuffle] (http://en.wikipedia.org/wiki/Fisher-Yates_shuffle)은 가능한 구현 중 하나입니다. –