Post

Replies

Boosts

Views

Activity

How to limit my numbers in Swift
Hello everyone, quick question, in Swift, how to limit numbers? I'm practicing on the creation of a game between 2 players, and I want the limit of health points of my characters to be exactly between 0 and 100. If my character loses health points, I don't want it to fall to -10 (negative) and on the contrary if he gains health points, I don't want it to exceed 100 health points. I'm an beginner, I'm starting with Swift There is my code : class Character {     var name: String     var lives: Int // health points     var power : Int     init(name: String, lives: Int, power: Int) {         self.name = name         self.lives = lives         self.power = power     }     convenience init(){         self.init(name:"", lives: 100, power: 0)     } // get heal //     func heal(anotherCharacter: Character){         if anotherCharacter.lives < 100{             anotherCharacter.lives += 20         }     } // attack another character //     func attack(otherCharacter: Character){         otherCharacter.lives -= power     } } // MARK: - Soldier A class SoldierA: Character{     override init(name: String, lives: Int, power: Int) {         super.init(name: "Daniel", lives: 100, power: 50)     } } // MARK: - Soldier B class SoldierB: Character{     override init(name: String, lives: Int, power: Int) {         super.init(name: "Mark", lives: 100, power: 50)     } } // MARK: - Fight let player1 = SoldierA() let player2 = SoldierB() player1.attack(otherCharacter: player2) player1.attack(otherCharacter: player2) player1.attack(otherCharacter: player2) player2.lives player1.heal(anotherCharacter: player1) player1.lives
11
0
1.9k
Jan ’21
How to access viewController components in model (MVC)
Hello, For my third iOS project, I want to use the MVC design pattern. Until now the logic of my application was in viewController. My project is a calculator. I want to put the logic in a class named :" Compute " and manage the buttons in viewControler. But the problem is that I need to use the UIAlertController and the textView in the Compute class but I don't have access. Here is some of my code the Compute class is empty for the moment.     // MARK: - Outlets     @IBOutlet weak var textView: UITextView!     @IBOutlet var numberButtons: [UIButton]!     // MARK: - Propriétés     var compute = Compute()          var elements: [String] {         return textView.text.split(separator: " ").map { "\($0)" }     }     // Error check computed variables     var expressionIsCorrect: Bool {         return elements.last != "+" && elements.last != "-"     }     //     var expressionHaveEnoughElement: Bool {         return elements.count >= 3     }     //     var canAddOperator: Bool {         return elements.last != "+" && elements.last != "-"     }     //     var expressionHaveResult: Bool {         return textView.text.firstIndex(of: "=") != nil     }     // View Life cycles     override func viewDidLoad() {         super.viewDidLoad()         // Do any additional setup after loading the view.     }     // MARK: - actions     @IBAction func tappedNumberButton(_ sender: UIButton) {         guard let numberText = sender.title(for: .normal) else {             return         }         if expressionHaveResult {             textView.text = ""         }         textView.text.append(numberText)     }     @IBAction func tappedAdditionButton(_ sender: UIButton) {         if canAddOperator {             textView!.text.append(" + ")         } else {             let alertVC = UIAlertController(title: "Zero!", message: "Already have an operator !", preferredStyle: .alert)             alertVC.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))             self.present(alertVC, animated: true, completion: nil)         }     }
3
0
586
Jun ’21
How to scroll UICollectionView cell automatically according to the time of day
Hello, Can you help me please ? I am creating a small application that displays the bus timetables of my city. I use UICollectionView to display all the timetables: from 06:00 in the morning to 00:30 in the evening. Buses pass every 30 min. Now I want my cells to scroll automatically according to the time of day, even if the application is closed. I want my cell that displays the next bus (e.g. 2:30 pm) to display at 2:00 pm and automatically scroll after 2:30 pm to display the next bus that comes at 3:00 pm etc. until 00:30 am. And after 00:30, I want to display the cell that shows 06:00). And the process continues ad infinitum. thank you There my code : `extension HomeViewController : UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return busHoursList.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let busCell = collectionView.dequeueReusableCell(withReuseIdentifier: "busCellID", for: indexPath) as! BusCollectionViewCell busCell.layer.borderWidth = 0.6 busCell.layer.cornerRadius = 15 busCell.layer.borderColor = CGColor.init(red: 170/250, green: 170/250, blue: 170/250, alpha: 1) busCell.setupDepartureHours(withText: busDepartureHoursList[indexPath.row])}
6
0
1.1k
Aug ’21