mardi 17 janvier 2017

Random Generation of Property From Dictionary Swift 2

I wanted to ask a question that has had me stuck for quite some time.

I have created a class with specific properties, and created a few objects to go with it (only 2 are shown in example)

 class ShapeColour : NSObject {
        var colour:String
        var shape:String
        var image:UIImage

init(color: String, shape: String, image: UIImage) {
            self.colour = color
            self.shape = shape
            self.image = image
        }

    }


    //Blue Square

    let blueSquareObject =  ShapeColour(color: "blue", shape: "square", image: UIImage(named: "BlueSquare.jpg")!)

    //Red Square

    let redSquareObject = ShapeColour(color: "red", shape: "sqaure", image: UIImage(named: "RedSquare.jpg")!)

Next, I created a dict containing all the objects (again, only 2 are shown) and wrote a line of code to try randomly select one of the objects.

let allMainShape = [0:blueSquareObject,1:blueCircleObject]
let randomIndex = Int(arc4random_uniform(UInt32(allMainShape.count)))

I want to know how I can randomly generate one of the objects, and from that generation extract one of the attribute. I thought it would be something like this:

print"Colour is \(allMainShape.color[randomIndex])"

Thanks, Any help would be appreciated greatly ;)




Aucun commentaire:

Enregistrer un commentaire