2016-07-25 87 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