Post

Replies

Boosts

Views

Activity

Reply to Problems with randomisation when appending to an array
It's very long, so I'm unable to post the portion from the model... If the mini-version I sent before is working, though, I may be able to just add bits until I find the problem :) View controller: import UIKit class HorseSalesViewController: UIViewController {     @IBOutlet weak var timeOutlet: UILabel!     @IBOutlet weak var cashOutlet: UILabel!          @IBOutlet weak var saleConfirmOutlet: UILabel!     @IBOutlet weak var horsePriceOutlet: UILabel!        override func viewDidLoad() {     super.viewDidLoad()  &#9;     cashOutlet.text = "Cash: \(cash)"         timeOutlet.text = "Time: \(time)"         horsePriceOutlet.text = "Cost: \(foundationHorsePrice)"     }          let bayRandom = bayArray.randomElement()     let blackRandom = blackArray.randomElement()     let buckskinRandom = buckskinArray.randomElement()     let chestnutRandom = chestnutArray.randomElement()     let cremelloRandom = cremelloArray.randomElement()     let dunRandom = dunArray.randomElement()     let perlinoRandom = perlinoArray.randomElement()     let smokyBlackRandom = smokyBlackArray.randomElement()     let smokyCreamRandom = smokyCreamArray.randomElement()     let greyRandom = greyArray.randomElement()     let overoRandom = overoArray.randomElement()     let palominoRandom = palominoArray.randomElement()     let roanRandom = roanArray.randomElement()     let sabinoRandom = sabinoArray.randomElement()     let maxSabinoRandom = maxSabinoArray.randomElement()     let tobianoRandom = tobianoArray.randomElement()     let headMarkingsRandom = headMarkingsArray.randomElement()     let legMarkingsRandom = legMarkingsArray.randomElement()     let blankImage =  imageLiteral(resourceName: "Blank image")          @IBAction func buyHorseButton(_ sender: UIButton) {         if cash > foundationHorsePrice {         cash -= foundationHorsePrice         saleConfirmOutlet.text = "Congratulations on your new horse!"         cashOutlet.text = "Cash:\(cash)"         myHorses.append(Horse(             //Base stats             name: "New Horse",             gender: ["Mare", "Stallion"][Int.random(in:0...1)],             age: Int.random(in:1...19),             focus: Int.random(in:10...100),             health: Int.random(in:1...100),             points: 0,             level: "Intro",             weight: Int.random(in:450...650),             reactivity: Int.random(in:1...100),             //Conformation             head: ["Poor", "Substandard", "Average", "Good", "Excellent"][Int.random(in:0...4)],             neck: ["Poor", "Substandard", "Average", "Good", "Excellent"][Int.random(in:0...4)],             back: ["Poor", "Substandard", "Average", "Good", "Excellent"][Int.random(in:0...4)],             legs: ["Poor", "Substandard", "Average", "Good", "Excellent"][Int.random(in:0...4)],             hindquarters: ["Poor", "Substandard", "Average", "Good", "Excellent"][Int.random(in:0...4)],                      //Pedigree             sire: "Unknown",             dam: "Unknown",                          //Training             rhythm: 0,             looseness: 0,             contact: 0,             impulsion: 0,             straightness: 0,             collection: 0,                        //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: chestnutRandom ??  imageLiteral(resourceName: "Blank image"),             roanPhenotype: roanRandom ??  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"),             //Feed requirements             water: 100,             energy: 100,             protein: 100,             vitamins: 100,             minerals: 100,             chewingTime: 100))         }                  if cash < foundationHorsePrice {             saleConfirmOutlet.text = "You don't have enough cash"         }          }
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to Problems with randomisation when appending to an array
The IBAction is the buyHorseButton. I tried adding the cash value to the horseName, and the horses came up random in the simulator :). However, when I removed the cash label, they turned out the same again. Maybe horses should have an identification number to differentiate them? I wanted to make a unique ID number for each horse anyway, maybe this is a good time to implement that
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to Problems with randomisation when appending to an array
Please print (at line 117) the array of horses after the first and second addition. And post the result. When I add the code "print(myHorses)" and run the IBAction, the console opens but there's nothing in it. Maybe the array is too long? 2. Show the code where you populate the array The array is populated with the buyHorseButton. There are a couple of sample horses in there already for testing purposes 3. Answer the other questions: What is exactly the problem: - same horse created several times (does not seem the case) - this is what happens, unless I add cash to the name. Maybe the problem will go away if I assign an ID number to each horse so that they have unique names? I was meaning to do this anyway. I'm not sure why it happens but the randomisation worked with cash added to the name, but not without. - they do not show randomly as you want in the table ? - the first horse is random, then the following horses are the same as the first one. When I close the simulator and run again, a new random horse is generated and then the following horses are clones of the first. 4. We do not see where you declared myHorses. Please show. The list of horses are shown in their own VC (shown below), and when a horse is clicked this opens a new VC with fields that fetch data from the myHorses array import UIKit class HerdViewController: UIViewController {     @IBOutlet weak var buttonsStack: UIStackView!        override func viewDidLoad() {         super.viewDidLoad()                  for horse in myHorses {             let button = UIButton()             button.addTarget(self, action: #selector(self.lookupHorse(sender:)), for: .touchUpInside)             button.setTitleColor(.white, for: UIControl.State.normal)             button.setTitle(horse.name, for: UIControl.State.normal)                          button.backgroundColor =  colorLiteral(red: 0.005273803137, green: 0.4785152674, blue: 0.3960535526, alpha: 1)             buttonsStack.addArrangedSubview(button)          }          buttonsStack.translatesAutoresizingMaskIntoConstraints = false              }          private var horseSelected: Horse?          @objc func lookupHorse(sender: UIButton){             let horseName = sender.title(for: .normal)             //`lookupHorse` from `myHorses`             if let theHorse = myHorses.first(where: {$0.name == horseName}) {                     horseSelected = theHorse                     self.performSegue(withIdentifier: "horseViewerSegue", sender: self)             }     }          override func prepare(for segue: UIStoryboardSegue, sender: Any?) {             switch segue.identifier {             case "horseViewerSegue":                     let horseVC = segue.destination as! HorseViewController                     horseVC.horsePassed = horseSelected             default:                     print("Unknown segue: \(segue.identifier ?? "nil")")             }     }      Do you have tips on how to create ID numbers for each new horse created? I have a feeling this can solve the issue. I'll add an ID number property to the buyHorseButton and assign some numbers to the sample horses
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to Problems with randomisation when appending to an array
I think I solved the issue. I added an ID number to each new horse, making all the names unique. Now each new horse is random and also has an ID number. I made a property : var horsesCreated = 2 then each new horse got the property idNumber: (horsesCreated+1) and the buyHorseButton is assigned to do this after creating a horse; horsesCreated += 1 I know this maybe isn't the cleanest fix, but I am really happy it's working. Couldn't have done it without your input! Thanks for your patience :)
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to Problems using classes for reusable headers
I did a clean build folder, but still getting this error: Thread 1: "[<UIView 0x7fb6b0d119b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key headerText." I tried commenting out the bit where I change the headerText and the outlet for the header without result. Also checked for broken connections in the view controller. It's underlining AppDelegate in the error text with the same error. There is still a headerText outlet in HeaderView.swift. This isn't connected to anything since I changed the class of the HeaderView.xib, I can't see the file in assistant anymore so I deleted the connection. Still getting the same error
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to Segue to Tabbed view controller crashes
Yeah, it makes more sense to ask it to search for a UITabBarController :) How do I find the index for kIndexHorseViewController? For now, I tested it with "1" as the index and it crashed with the same error at let horseVC = tabBarVC.viewControllers![kIndexHorseViewController] as! HorseViewController
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’20