2

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?)

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Arbab Rizvi
  • 177
  • 1
  • 2
  • 10
  • Create an array. Don't even think there is some better solution. Creating a class for an entrepreneur is good but the actual data should be in an external file - e.g. JSON or XML instead of hardcoding them. – Sulthan Dec 13 '15 at 08:52
  • Oh right I should have it in xml format in a differnt file, even if I am hard coding it I should har code it in xml since there is no api I can find with the exact information that I need – Arbab Rizvi Dec 13 '15 at 22:22

1 Answers1

0

Seems to me like you want to randomly select objects from a group of them. To do this you need an array and a random number between 0 and the amount of elements in the array.

Code

let array: [Entrepreneurs] = [a,b,c,...]
let random: Int = Int(arc4random_uniform(UInt32(array.count)))
let selection: Entrepreneurs = array[random]

As far as showing them through and alert I need more details on the question.

Cody Weaver
  • 4,756
  • 11
  • 33
  • 51
  • Hey Cody, Im not sure I understand your solution. I agree that we need to create an array that will hold the members or objects of the entrepreneur class. Here you declare an array of type [Entrepreneurs] and = it to [a, b, c]. But what exactly should go in the a, b, c? I do not want to add each entreprenuer, ex, zuckerberg, in the array manually, that will take very long and I plan to have hundreds of entpreneurs in here eventually. – Arbab Rizvi Dec 14 '15 at 04:16
  • Can you please mark my answer as correct. Yes a,b,c are instances of Entrepreneur. Also I think you should make it a struct not a class. I understand that "manually" adding them into the array sucks but at some point you have to input them into the machine. – Cody Weaver Dec 14 '15 at 06:12
  • @ArbabRizvi What part are you stuck on? – Cody Weaver Dec 14 '15 at 07:02