Skip to content

Instantly share code, notes, and snippets.

@intan1907
Created June 16, 2021 05:14
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 intan1907/b10ec5f8efd55fe46fb2eab2562a8c3f to your computer and use it in GitHub Desktop.
Save intan1907/b10ec5f8efd55fe46fb2eab2562a8c3f to your computer and use it in GitHub Desktop.
Function to Randomize Recipe
import Foundation
public var recipeData: [RecipeBaseClass]? = loadJsonFromFile()
// load dummy data from json file
func loadJsonFromFile(fileName: String = "Recipes") -> [RecipeBaseClass]? {
// ...
}
public func getRandomRecipe() -> RecipeBaseClass? {
guard !(recipeData?.isEmpty ?? true) else {
return nil
}
let randomIndex = Int.random(in: 0..<(recipeData?.count ?? 0))
return recipeData?[randomIndex]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment