2016-07-25 1 views
1

私はこの配列をシャッフルしようとしています(私はhttps://github.com/inspace-io/INSPhotoGallery拡張子使用しています):私はあなたが使用してint配列をシャッフルすることができます知っているINSPhotoViewable(カスタム)配列をシャッフルする方法は?

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")), 
]() 

extension CollectionType { 
    /// Return a copy of `self` with its elements shuffled 
    func shuffle() -> [Generator.Element] { 
     var list = Array(self) 
     list.shuffleInPlace() 
     return list 
    } 
} 

extension MutableCollectionType 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 { 
      let j = Int(arc4random_uniform(UInt32(count - i))) + i 
      guard i != j else { continue } 
      swap(&self[i], &self[j]) 
     } 
    } 
} 

[1, 2, 3].shuffle() 

私はINSPhotoViewable配列をシャッフルする方法で失われています。

更新: ここに私のコードです。それはエラーなしで実行するようだが、そのシャッフリングない:

extension CollectionType { 

    /// Return a copy of `self` with its elements shuffled 
    func shuffle() -> [Generator.Element] { 
     var list = Array(self) 
     list.shuffleInPlace() 
     return list 
    } 
} 

extension MutableCollectionType 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 { 
      let j = Int(arc4random_uniform(UInt32(count - i))) + i 
      guard i != 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() 

     for photo in photos { 
      if let photo = photo as? INSPhoto { 
       photo.attributedTitle = NSAttributedString(string: "Note: Click top right to download wallpaper, \nscroll left or right to browse", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()]) 
      } 
     } 


    } 


} 
+0

問題点を教えてください。 – Alexander

+1

NSViewの配列でテストしてみました。カスタムクラスでもうまくいくはずです。 – Moritz

+0

コードを変更しましたか、それとも単に機能しましたか? – Brandex07

答えて

1

あなたの拡張戻りシャッフル配列、それは場所既存のものにシャッフルされません。あなたは変数にシャッフル配列を維持する必要がない場合

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

を、または

let shuffled = photos.shuffle() 

for photo in shuffled { 
    // work 
} 

あなたがしなければ

だから、あなたが行うことができます。

説明:extension CollectionTypelist.shuffleInPlace()では、アレイのコピーで動作する - それは、その後シャッフルコピーを返します。

+0

Ohh ok私は愚かだと感じる。それは明らかだったはずです。ご協力ありがとうございました!! – Brandex07

+1

Eh。どういたしまして。 :) – Moritz

関連する問題