如何改组INSPhotoViewable(自定义)数组?

Brandex07

我正在尝试改组此数组(我正在使用https://github.com/inspace-io/INSPhotoGallery扩展名):

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()])
            }
        }


    }


}
埃里克·艾雅(Eric Aya)

您的扩展程序返回一个改组后的数组,它不会对现有的数组进行改组。

因此,您可以执行以下操作:

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

如果您不需要将改组后的数组保留在变量中,或者

let shuffled = photos.shuffle()

for photo in shuffled {
    // work
}

如果可以的话。

说明:extension CollectionTypelist.shuffleInPlace()上一个工作拷贝数组-它然后返回洗牌后的副本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何调用自定义Java数组

来自分类Dev

如何自定义多维数组?

来自分类Dev

如何定义自定义接口数组的数组

来自分类Dev

如何在PHP数组中定义自定义键?

来自分类Dev

如何在Go中创建自定义类型元素的自定义类型数组?

来自分类Dev

如何在自定义视图中向多数据添加自定义数组?

来自分类Dev

如何在自定义视图中向多数据添加自定义数组?

来自分类Dev

自定义尺寸数组

来自分类Dev

如何初始化自定义对象的数组

来自分类Dev

如何存储自定义对象数组(目标)

来自分类Dev

如何初始化自定义类对象的数组?

来自分类Dev

如何在Powershell中创建自定义数组?

来自分类Dev

如何重载Base.show自定义数组类型?

来自分类Dev

Swift Codable-如何编码自定义数组

来自分类Dev

如何在python中打印自定义对象的数组?

来自分类Dev

如何使用自定义键遍历数组

来自分类Dev

如何从plist文件读取自定义对象的数组?

来自分类Dev

如何从数组项获取自定义值

来自分类Dev

如何使用NSUserDefaults取消存档自定义数组?

来自分类Dev

如何从数组中提取自定义文本

来自分类Dev

如何使用自定义请求验证laravel数组?

来自分类Dev

如何以自定义方式对多维数组进行排序

来自分类Dev

如何使用自定义JsonSerializerSettings处理数组创建?

来自分类Dev

如何向MySQL Fetch数组添加自定义值

来自分类Dev

如何在mongdb中自定义数组

来自分类Dev

如何根据条件过滤自定义对象数组

来自分类Dev

如何向自定义对象数组添加条目

来自分类Dev

如何使用javascript创建自定义数组

来自分类Dev

我如何推送到自定义类型数组

Related 相关文章

热门标签

归档