Learn Courses My Dashboard

Help to make a program that picks a sentence from a list

Hello! I need help to make a sentence picker for my AR game. I need help to find a script to picks a sentence from a list. Can someone give me the script to do this and/or tell me what its means? Thank you!

Welcome to the community!!

Like picking a random element from an array?

1 Like

Yes, I think so. Can you give me example code to do this? Thank you! (Thank you for the reply)

let array = ["String 1", "String 2", "String 3"]
let randomString = array.randomElement()

//but be aware that randomString is an Optional since you could call
//randomElement() on an empty array and it will return nil

You could also shuffle the array, and then choose the first element

Thank you for the code, but it gives me an error. Can you help me fix it?

You need to set a variable for your randomElement and use the value

Also I would highly recommend going back to iOS foundations and learning more about how arrays work.

Thank you for helping me these few days!