Created
June 16, 2021 05:14
-
-
Save intan1907/b10ec5f8efd55fe46fb2eab2562a8c3f to your computer and use it in GitHub Desktop.
Function to Randomize Recipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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