-1

I have a question. How can i do it, that my array picks randomizely a String from the list?

I want to pick random one of these products of the list?

My thought was, i can do it again with an array, but I get this error:

EXC_BAD_INSTRUCTION (code=EXC_i386_INVOP, subcode=0x0

My Code is :

private func randomProduct() -> Int {
let myArray1 = ["Nintendo Classic Mini NES", "Microsoft Office 2016 Home and Business", "Rubie's Deluxe Muscle Chest Batman Child", "Giro Dime", "Varta Powersports AGM 12V 12Ah 512014010A514", "Pohl-Boskamp Gelomyrtol Forte", "Panasonic ES-LV9N-S803", "Traxxas X-Maxx (77076-4)", "GoPro HERO 4", "Bose Lifestyle 650", "WMF Function 4 Kochtopf-Set 4-teilig", "Microsoft Surface Book", "Hewlett-Packard HP Color LaserJet Pro M252dw (B4A22A)", "Apple iPhone 7", "X-lite X-403GT Elegance", "Denon AVR-X3300W", "Hasbro Spiel des Lebens Banking", "Avent SCD 630/26", "Ray-Ban Aviator Metal RB3025"]
return Int(myArray1[Int(arc4random_uniform(UInt32(myArray1.count)))])!

Hope you can help me!

Thx a lot!

Shades
  • 5,568
  • 7
  • 30
  • 48
Saintz
  • 69
  • 7

1 Answers1

0

You're close, but you want to use the random number Int as the index to find your String within the array. Then you return the string. So, just make a couple changes:

private func randomProduct() -> String { // Return String, not Int
    let myArray1 = ["Nintendo Classic Mini NES", "Microsoft Office 2016 Home and Business", "Rubie's Deluxe Muscle Chest Batman Child", "Giro Dime", "Varta Powersports AGM 12V 12Ah 512014010A514", "Pohl-Boskamp Gelomyrtol Forte", "Panasonic ES-LV9N-S803", "Traxxas X-Maxx (77076-4)", "GoPro HERO 4", "Bose Lifestyle 650", "WMF Function 4 Kochtopf-Set 4-teilig", "Microsoft Surface Book", "Hewlett-Packard HP Color LaserJet Pro M252dw (B4A22A)", "Apple iPhone 7", "X-lite X-403GT Elegance", "Denon AVR-X3300W", "Hasbro Spiel des Lebens Banking", "Avent SCD 630/26", "Ray-Ban Aviator Metal RB3025"]
    return myArray1[Int(arc4random_uniform(UInt32(myArray1.count)))] // Remove the Int()
}
Shades
  • 5,568
  • 7
  • 30
  • 48