Can you append an array with an if statement?

I want to add an item to an array, where one element depends on the result of another. Not sure if this can even work, but I haven't been able to test it since I'm not sure how to formulate an If statement to appending an array. Any help appreciated :)


appending the array
Code Block
myHorses.append(Horse(
//genotype 
gre: ["G", "g"][Int.random(in:0...1)]+["G", "g"][Int.random(in:0...1)],
//phenotype
greyPhenotype :  UIImage //this is where I would add the if statement
))


the if statement
Code Block
if gre == "Gg" {
greyPhenotype : greyArray.randomElement()
}
if gre == "GG" {
greyPhenotype : greyArray.randomElement()
}
if gre == "gg" {
greyPhenotype : blankImage
}


you need to have the if outside:

Code Block
let genotype = ["G", "g"][Int.random(in:0...1)]+["G", "g"][Int.random(in:0...1)]
var phenotype :  UIImage
switch genotype {
case "Gg": phenotype = greyArray.randomElement()
case "GG" : phenotype = greyArray.randomElement()
case "gg" : phenotype = : blankImage
default : phenotype = : blankImage // that will handle the "gG" case…
}
myHorses.append(Horse(
gre: genotype,
greyPhenotype : phenotype
))


If this works, thanks to mark the answer, not your own reply…
Thanks! I was going to mark your reply as the answer but missed...! I wasn't able to unmark it but I'll try again
Where are you putting the switch statement? I have it just above the IBAction that triggers the append but I get an error "expected declaration" at the start of the switch statement

I reworded the code to make it easier to distinguish all the different genes that will be in play later.


Code Block
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
//Genotype
let greGenotype = ["G", "g"][Int.random(in:0...1)]+["G", "g"][Int.random(in:0...1)]
    //Phenotype
    var grePhenotype : UIImage
    switch greGenotype {
        case "Gg" : grePhenotype = UIImage(greyArray.randomElement()!)
        case "gG" : grePhenotype = UIImage(greyArray.randomElement()!)
        case "GG" : grePhenotype = UIImage(greyArray.randomElement()!)
        case "gg" : grePhenotype =  : blankImage
        default :  grePhenotype =  : blankImage
    }


IBAction
Code Block
myHorses.append(Horse(
gre: greGenotype,
greyPhenotype : grePhenotype,))



Where are putting the append ?
This code should go at the same place. So, in the IBAction.

But you did not use my code, but changed it.

I wrote:
Code Block
switch genotype {


you typed :
Code Block
    switch greGenotype {


Very different.
I have the IBAction in the section below viewDidLoad. When I move it into viewDidLoad, I get errors:


Code Block  switch greGenotype {
        case "Gg" : grePhenotype = UIImage(greyArray.randomElement()!) //No exact matches in call to initialize
        case "gG" : grePhenotype = UIImage(greyArray.randomElement()!) //No exact matches in call to initialize
        case "GG" : grePhenotype = UIImage(greyArray.randomElement()!) //No exact matches in call to initialize
        case "gg" : grePhenotype =  : blankImage //Expected expression in assignment
        default :  grePhenotype =  : blankImage //Expected expression in assignment
    }
    @IBAction func buyHorseButton(_ sender: UIButton) { //Only instance methods can be declared @IBAction


I had to change the code to match the rest of the app so I could test it. everything should work though

Please show the complete code, so we can understand exactly the context.

You say you get errors.
Which, on which line ?

// Only instance methods can be declared @IBAction

Are you sure you don't miss a closing curly bracket somewhere, or have it misplaced ?
That's typically the case when you get the error message shown above.

When I look at your code, the } on line 6 closes the switch.
You miss a } to close the viewDidload before calling iBACtion

Here is the code:

Code Block
import UIKit
class HorseSalesViewController: UIViewController {. //Error: Class 'HorseSalesViewController' has no initializers
 override func viewDidLoad() {
        super.viewDidLoad()
}
//Genotype
    let extGenotype = ["E", "e"][Int.random(in:0...1)]+["E", "e"][Int.random(in:0...1)]
    let agoGenotype = ["A", "a"][Int.random(in:0...1)]+["A", "a"][Int.random(in:0...1)]
    let greGenotype = ["G", "g"][Int.random(in:0...1)]+["G", "g"][Int.random(in:0...1)]
    let creGenotype = ["Cr", "cr"][Int.random(in:0...1)]+["Cr", "cr"][Int.random(in:0...1)]
    let dunGenotype = ["D", "nd2"][Int.random(in:0...1)]+["D", "nd2"][Int.random(in:0...1)]
    let tobGenotype = ["To", "to"][Int.random(in:0...1)]+["To", "to"][Int.random(in:0...1)]
    let sabGenotype =  ["SB1", "sb1"][Int.random(in:0...1)]+["SB1", "sb1"][Int.random(in:0...1)]
    let roaGenotype = ["Rn", "rn"][Int.random(in:0...1)]+["Rn", "rn"][Int.random(in:0...1)]
//Phenotype
    var grePhenotype : UIImage //Error: 1. Stored property 'grePhenotype' without initial value prevents synthesized initializers
    switch greGenotype { //Error: Expected declaration
        case "Gg" : grePhenotype = UIImage(greyArray.randomElement()!)
        case "gG" : grePhenotype = UIImage(greyArray.randomElement()!)
        case "GG" : grePhenotype = UIImage(greyArray.randomElement()!)
        case "gg" : grePhenotype =  : blankImage
        default :  grePhenotype =  : blankImage
    }
@IBAction func buyHorseButton(_ sender: UIButton) {
        myHorses.append(Horse(
//Genotype
            ext: extGenotype,
            ago: agoGenotype,
            gre: greGenotype,
            cre: creGenotype,
            dun: dunGenotype,
            tob: tobGenotype,
            sab: sabGenotype,
            roa: roaGenotype,
            //Phenotype
            basePhenotype :  imageLiteral(resourceName: "Blank image.png"),
            roanPhenotype :  imageLiteral(resourceName: "Blank image.png"),
            greyPhenotype : grePhenotype,
            tobianoPhenotype :  imageLiteral(resourceName: "Blank image.png"),
            sabinoPhenotype :  imageLiteral(resourceName: "Blank image.png"),
            whiteFacePhenotype :  imageLiteral(resourceName: "Blank image.png"),
            whiteLegsPhenotype :  imageLiteral(resourceName: "Blank image.png"))
}
}




    
Can you append an array with an if statement?
 
 
Q