samedi 12 décembre 2015

Randomly select an instantiation of a class and display it?

Goal: Randomly select a member of a class containing people, and display the information in an alert.

Here is what I am doing: I have a class called Entrepreneurs. Inside the class are variables for name, networth etc. I have instantiated the class with several Entrepreneurs as follows:

   //Class for entrepreneurs
class Entrepreneurs {
    var name:String?
    var netWorth = (0.0, "")
    var company:String?
    var summary: [String]
    var age: Int?
    init() {
        name = ""
        company = ""
        summary = [""]
        age = 1;
    }
}

 let markZuckerBerg = Entrepreneurs()
    markZuckerBerg.name = "Mark Zuckerberg"
    markZuckerBerg.age = 19
    markZuckerBerg.company = "Facebook"
    markZuckerBerg.netWorth = (35.7, "Billion")

I have several instantiations (more than 5) and I now want to randomly access a member from the Entrepreneur class and display its properties.

I know I will need an Array to hold the class members but I don't think adding each member of the class in an array is the most efficient way to go about it, since I plan on adding hundreds of entrepreneurs eventually.

Any Suggestions?

(Side question: is this the best way to structure such a problem? In other words, If my goal is to have a list of entrepreneurs with information about them, and I want them to display randomly on the screen of a phone as an alert, is creating a class of entrepreneurs the best way?)




Aucun commentaire:

Enregistrer un commentaire