2016-07-25 79 views
1

我試圖打亂這個數組(我用https://github.com/inspace-io/INSPhotoGallery擴展名):如何洗牌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")), 
]() 

我知道,你可以洗牌使用int數組

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

我剛剛測試了一系列NSViews,它工作。它也應該適用於你的自定義類。 – Moritz

+0

你是否改變了代碼,或者它只是工作? – Brandex07

回答

1

您的擴展回報改組陣列,它不會取代現有的洗牌。

所以,你可以這樣做:

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

,如果你不需要保持洗牌數組中的變量,或

let shuffled = photos.shuffle() 

for photo in shuffled { 
    // work 
} 

如果你這樣做。

說明:在extension CollectionType中,list.shuffleInPlace()對數組的副本進行操作,然後返回混洗副本。

+0

哦,好吧,我感到很蠢。這應該是顯而易見的。感謝您的幫助!! – Brandex07

+1

呃。別客氣。 :) – Moritz