Skip to content

Instantly share code, notes, and snippets.

@Zeta611
Last active April 8, 2019 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zeta611/6e7d639677d447b2dfac99e1f66fa05b to your computer and use it in GitHub Desktop.
Save Zeta611/6e7d639677d447b2dfac99e1f66fa05b to your computer and use it in GitHub Desktop.
[Array+removeRandom] #extension #iOS
//
// Array+removeRandom.swift
//
// Created by Jay Lee on 31/01/2019.
// Copyright © 2019 Jay Lee <jaeho.lee@snu.ac.kr>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
//
import Foundation
extension Array {
mutating func removeRandom() -> Element? {
if count > 0 {
let randomIndex = Int.random(in: 0 ..< count)
return remove(at: randomIndex)
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment