ios - How to shuffle a INSPhotoViewable (custom) array? -


i trying shuffle array (i using https://github.com/inspace-io/insphotogallery extension):

lazy var photos: [insphotoviewable] = {         return [             insphoto(imageurl: nsurl(string: "http://i.imgur.com/jxy2d4a.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/jxy2d4a.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/nc4bqlb.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/nc4bqlb.jpg")), ]() 

i know can shuffle int array using

extension collectiontype {     /// return copy of `self` elements shuffled     func shuffle() -> [generator.element] {         var list = array(self)         list.shuffleinplace()         return list     } }  extension mutablecollectiontype index == int {     /// shuffle elements of `self` in-place.     mutating func shuffleinplace() {         // empty , single-element collections don't shuffle         if count < 2 { return }          in 0..<count - 1 {             let j = int(arc4random_uniform(uint32(count - i))) +             guard != j else { continue }             swap(&self[i], &self[j])         }     } }  [1, 2, 3].shuffle() 

i lost in how shuffle insphotoviewable array.

update: here code. seems run without errors not shuffling:

extension collectiontype {      /// return copy of `self` elements shuffled     func shuffle() -> [generator.element] {         var list = array(self)         list.shuffleinplace()         return list     } }  extension mutablecollectiontype index == int {     /// shuffle elements of `self` in-place.     mutating func shuffleinplace() {         // empty , single-element collections don't shuffle         if count < 2 { return }          in 0..<count - 1 {             let j = int(arc4random_uniform(uint32(count - i))) +             guard != j else { continue }             swap(&self[i], &self[j])         }     } }  class viewcontroller: uiviewcontroller {       @iboutlet weak var collectionview: uicollectionview!     var usecustomoverlay = false       lazy var photos: [insphotoviewable] = {         return [             insphoto(imageurl: nsurl(string: "http://i.imgur.com/jxy2d4a.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/jxy2d4a.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/nc4bqlb.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/nc4bqlb.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/jbbqxnz.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/jbbqxnz.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/wcxkdww.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/wcxkdww.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/p7ujk0t.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/p7ujk0t.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/ak4qwss.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/ak4qwss.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/w2jjtdf.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/w2jjtdf.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/hccsco3.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/hccsco3.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/za6ialf.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/za6ialf.jpg")),              insphoto(imageurl: nsurl(string: "http://i.imgur.com/pqc6k4v.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/pqc6k4v.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/d8bbmd4.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/d8bbmd4.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/bggxrss.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/bggxrss.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/w1lnl2c.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/w1lnl2c.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/qoa0qa9.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/qoa0qa9.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/dkcefkw.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/dkcefkw.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/u4ihoo6.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/u4ihoo6.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/qvlbs7a.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/qvlbs7a.jpg")),             insphoto(imageurl: nsurl(string: "http://i.imgur.com/zytdik1.jpg"), thumbnailimageurl: nsurl(string: "http://i.imgur.com/zytdik1.jpg")),          ]     }()      override func viewdidload() {         super.viewdidload()         collectionview.delegate = self         collectionview.datasource = self          photos.shuffle()          photo in photos {             if let photo = photo as? insphoto {                 photo.attributedtitle = nsattributedstring(string: "note: click top right download wallpaper, \nscroll left or right browse", attributes: [nsforegroundcolorattributename: uicolor.whitecolor()])             }         }       }   } 

your extension returns shuffled array, doesn't shuffle in place existing one.

so, can do:

for photo in photos.shuffle() {     // work } 

if don't need keep shuffled array in variable, or

let shuffled = photos.shuffle()  photo in shuffled {     // work } 

if do.

explanation: in extension collectiontype list.shuffleinplace() operates on copy of array - returns shuffled copy.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -