Adding a random image from image array to another array

I am creating horses in a horse simulator by appending an array with:


Code Block
Horses.append(Horse(
//Genotype (two letters)
ext: ["E", "e"][Int.random(in:0...1)]+["E", "e"][Int.random(in:0...1)],
//Phenotype (an image)
      basePhenotype: [bayArray.randomElement()],


But the basePhenotype property is set to be a UIImage. Even though bayArray is only images, it won't accept this array as an input. How can I write it differently?
Answered by Claude31 in 631970022
I don't understand how your create tobianoArray.

But this should work:

Code Block
let img1 = UIImage(imageLiteralResourceName: "Tobiano1")
let img2 = UIImage(imageLiteralResourceName: "Tobiano2")
let img3 = UIImage(imageLiteralResourceName: "Tobiano3")
let tobianoArray = [img1, img2, img3]
let pheno = tobianoArray.randomElement() // Take care, that's an optional
// You could use ?? :
// let pheno = tobianoArray.randomElement() ?? UIImage(imageLiteralResourceName: "defaultImage")

But the basePhenotype property is set to be a UIImage. Even though bayArray is only images

It is not clear enough how you have declared them. Please show the code.
More code:
Code Block
struct Horse {
//Genotype
    var ext : String
 //Phenotype
    var basePhenotype : UIImage

Is there another way I should be declaring the basePhenotype property?
You show so little code it is hard to understand what you do.

But, basePhenotype is UIImage
Code Block
var basePhenotype - UIImage

Hence, it cannot be an array
Code Block
basePhenotype: [bayArray.randomElement()]

Note: I don't understand
  • is it a declaration or assignment ?

  • what the comma is after this statement…

May be you just want:

Code Block
basePhenotype = bayArray.randomElement() // May have to use ?? to assign something if array is empty and unwrap the optional

Ok, I'll try to elaborate a bit. It is complex so I'm trying to boil it down to the most basic parts.
Here is some of the struct:

Code Block
struct Horse {
    //Genotype
    var ext : String
    var ago : String
    var gre : String
    var cre : String
    var dun : String
    var tob : String
    var sab : String
    var roa : String
    
    //Phenotype
    var basePhenotype : UIImage
    var roanPhenotype : UIImage
    var greyPhenotype : UIImage
    var tobianoPhenotype : UIImage
    var sabinoPhenotype : UIImage
    var whiteFacePhenotype : UIImage
    var whiteLegsPhenotype : UIImage
}


Here is the part where each horse is created in the view controller:

Code Block
myHorses.append(Horse(
            //Genotype
            ext: ["E", "e"][Int.random(in:0...1)]+["E", "e"][Int.random(in:0...1)],
            ago: ["A", "a"][Int.random(in:0...1)]+["A", "a"][Int.random(in:0...1)],
            gre: ["G", "g"][Int.random(in:0...1)]+["G", "g"][Int.random(in:0...1)],
            cre: ["Cr", "cr"][Int.random(in:0...1)]+["Cr", "cr"][Int.random(in:0...1)],
            dun: ["D", "nd2"][Int.random(in:0...1)]+["D", "nd2"][Int.random(in:0...1)],
            tob: ["To", "to"][Int.random(in:0...1)]+["To", "to"][Int.random(in:0...1)],
            sab: ["SB1", "sb1"][Int.random(in:0...1)]+["SB1", "sb1"][Int.random(in:0...1)],
            roa: ["Rn", "rn"][Int.random(in:0...1)]+["Rn", "rn"][Int.random(in:0...1)],
          
            //Phenotype
            basePhenotype: imageLiteral(resourceName: "Bay3.png"),
            roanPhenotype:  imageLiteral(resourceName: "Blank image.png"),
            greyPhenotype:  imageLiteral(resourceName: "Blank image.png"),
            tobianoPhenotype:  imageLiteral(resourceName: "Blank image.png"),
            sabinoPhenotype:  imageLiteral(resourceName: "Blank image.png"),
            whiteFacePhenotype:  imageLiteral(resourceName: "Blank image.png"),
            whiteLegsPhenotype:  imageLiteral(resourceName: "Blank image.png")
)


Horses are saved to an array that would look something like this:

Code Block
var myHorses = [
    Horse(ext: "Ee", ago: "Aa", gre: "gg", cre: "crcr", dun: "nd2nd2", tob: "toto", sab: "sb1sb1", roa: "rnrn", basePhenotype:  imageLiteral(resourceName: "Bay8"), roanPhenotype:  imageLiteral(resourceName: "Blank image"), greyPhenotype:   imageLiteral(resourceName: "Blank image"), tobianoPhenotype:   imageLiteral(resourceName: "Blank image"), sabinoPhenotype:   imageLiteral(resourceName: "Blank image"), whiteFacePhenotype:  imageLiteral(resourceName: "Head7"), whiteLegsPhenotype:  imageLiteral(resourceName: "Leg11")


So what happens on creation is that each horse is assigned two alleles ( for example "E" or "e") per gene (genes: ext, ago, gre). Based on the alleles, an array is chosen. I have done something similar in a different app, but didn't save the results in a "myHorses" array like I am doing here. In the other app, it was interacting directly with the image in the view controller. This time I want to save the results in the myHorses array instead.

Code Block
    //Base coat image
        switch (ext, ago, cre) {
            case
            ("EE", "aa", "crcr"),
            ("Ee", "aa", "crcr"):
            baseLayer.image = blackArray.randomElement()
            base = String("black ")
          
            case
            ("ee", "aa", "crcr"),
            ("ee", "Aa", "crcr"),
            ("ee", "AA", "crcr"):
            baseLayer.image = chestnutArray.randomElement()
            base = String("chestnut ")
                    
            case
            ("EE", "AA", "crcr"),
            ("Ee", "AA", "crcr"),
            ("EE", "Aa", "crcr"),
            ("Ee", "Aa", "crcr"):
            baseLayer.image = bayArray.randomElement()
            base = String("bay ")


Let me know if there's anything else I can clear up! I tried to add as much code as possible
So, where is the problem ? What does not work ? What do you get ? What do you expect ?
The first challenge is to find a way to choose an image for each bit of the phenotype at random image. For example:

Code Block
//Phenotype             
roanPhenotype:  imageLiteral(resourceName: "Blank image.png"),      //random image from roanArray     
greyPhenotype:  imageLiteral(resourceName: "Blank image.png"),            //random image from greyArray      
tobianoPhenotype:  imageLiteral(resourceName: "Blank image.png"),             //random image from tobianoArray     


The next step is being able to base those array choices on the genes that are randomly chosen in genotype. I think I can do this with an If statement or a Switch statement, but I haven't been successful in choosing a random image, I have only been able to use a single image in the phenotype

The dream:

Code Block
myHorses.append(Horse(           
//Genotype - this part seems to work ok, could maybe be cleaner
        tob: ["To", "to"][Int.random(in:0...1)]+["To", "to"][Int.random(in:0...1)],
//Phenotype - this part doesn't work
if tob == ToTo {
tobianoPhenotype:  imageLiteral: tobianoArray.randomElement(),
}
if tob == Toto {
tobianoPhenotype:  imageLiteral: tobianoArray.randomElement(),
}
if tob == toto {
tobianoPhenotype:  imageLiteral: tobianoArray.randomElement(),
}


Did you try:

Code Block
tobianoArray  = [roanPhenotype, greyPhenotype,  tobianoPhenotype]

I have an array with horses:

Code Block
var myHorses = [
    Horse(genotype: "Toto", phenotype: "imageLiteral(resourceName: "Tobiano2")"
]


I also have an array for each phenotype;

Code Block
let tobianoArray = [ imageLiteral(resourceName: "Tobiano1"),  imageLiteral(resourceName: "Tobiano2"),  imageLiteral(resourceName: "Tobiano3")]


I want to take one item from this array to go in the Tobiano phenotype IF the horse has the correct genotype ("ToTo" or "Toto", not "toto"). A button adds a horse using the following:

Code Block
myHorses.append(Horse(
//genotype
tob: ["To", "to"][Int.random(in:0...1)]+["To", "to"][Int.random(in:0...1)],
//phenotype
tobianoPhenotype:  imageLiteral(resourceName: "Blank image.png"), // <-- this is the problem; how can I make this select a random item from tobianoArray?



Accepted Answer
I don't understand how your create tobianoArray.

But this should work:

Code Block
let img1 = UIImage(imageLiteralResourceName: "Tobiano1")
let img2 = UIImage(imageLiteralResourceName: "Tobiano2")
let img3 = UIImage(imageLiteralResourceName: "Tobiano3")
let tobianoArray = [img1, img2, img3]
let pheno = tobianoArray.randomElement() // Take care, that's an optional
// You could use ?? :
// let pheno = tobianoArray.randomElement() ?? UIImage(imageLiteralResourceName: "defaultImage")

Fantastic! Now I can get started working on the If statements :D Thank you so much for the help, I really appreciate your patience!
Adding a random image from image array to another array
 
 
Q