Ben's Swift Programming Adventure[Journal]

Moment 1 - First Debug

Hi everyone

Recently, I have been working on Module 3 of Chris’s beginner course.
Yesterday I ran into a bug that made the view display 18 cards instead of 16.
Here’s the all the code with the bug:

import Foundation

class CardModel {
    
    func getCards() -> [Card] {
        
        // Declare an empty array
        var generatedCards  = [Card]()
        
        // This is just for making sure that there are only 8 different numbers
        var generatedNumbers = [Int]()
        
        // Generate 8 pairs of cards
        while generatedNumbers.count <= 8 {
            
            // Generate a random Number
            let randomNumber = Int.random(in: 1...13)
            
            if generatedNumbers.contains(randomNumber) == false {
                
                // Create 2 new card objects
                let cardOne = Card()
                let cardTwo = Card()
                
                // Add the random number to the generated numbers list
                generatedNumbers.append(randomNumber)
                
                // Set their image names
                cardOne.imageName = "card\(randomNumber)"
                cardTwo.imageName = "card\(randomNumber)"
                
                // Add them to the array
                generatedCards += [cardOne, cardTwo]
                
                
            }
            
        }
        
        // Randomize the cards within the array
        generatedCards.shuffle()
        print(generatedNumbers)
        
        // Return the array
        return generatedCards
       
        
    }
    
}

I racked my head thinking of ways that I “achieved” this bug, I even did the latest coding challenge to help, but my code was the same as the one with the challenge(I figured out the challenge, but I don’t understand why the error would create 16, not any other number).
After spending about half an hour on it, I decided that I should leave this for the next day, so I can start with a fresh mind.
Unfortunately, in the morning I couldn’t work on it as I was busy with my school e-learning(yes, I’m still in school, I’m not even in middle school yet :grin: :stuck_out_tongue_winking_eye:), so I couldn’t work on the bug.
After a day of learning, I finally got time for debugging. This time I checked the while loop very carefully, and noticed that it had <= 8, I quickly realised that Swift is 0-based, which meant that <= 8 meant 9 times. I quickly fixed that mistake and debugged my first bug, ON MY OWN :laughing:!
Turns out that “// This is just for making sure that there are only 8 different numbers” can actually mean 9 numbers.
Debugged code for ViewController.swift:
import Foundation

class CardModel {
    
    func getCards() -> [Card] {
        
        // Declare an empty array
        var generatedCards  = [Card]()
        
        // This is just for making sure that there are only 8 different numbers
        var generatedNumbers = [Int]()
        
        // Generate 8 pairs of cards
        while generatedNumbers.count <= 7 {
            
            // Generate a random Number
            let randomNumber = Int.random(in: 1...13)
            
            if generatedNumbers.contains(randomNumber) == false {
                
                // Create 2 new card objects
                let cardOne = Card()
                let cardTwo = Card()
                
                // Add the random number to the generated numbers list
                generatedNumbers.append(randomNumber)
                
                // Set their image names
                cardOne.imageName = "card\(randomNumber)"
                cardTwo.imageName = "card\(randomNumber)"
                
                // Add them to the array
                generatedCards += [cardOne, cardTwo]
                
                
            }
            
        }
        
        // Randomize the cards within the array
        generatedCards.shuffle()
        print(generatedNumbers)
        
        // Return the array
        return generatedCards
       
        
    }
    
}

Ben

Great job working through that bug!

Many parts of programming can be very tedious, gotta make sure you pay attention!

Moment 2 - Damned Lines

Hello everyone,

Throughout the past week, I met a damned bug that left me stumped.
It was in the Quizapp, and the tableView cells kept being at the same length.

And I checked all the code, but the code was right.

So then after 3 days, I raged and stopped checking for another 3 days.

Just now, my mind reminded me of the lines attribute in the label cell.

For some reason, I left it as 1 instead of 0, and it kept me stupefied for so long.

Here are the screenshots:

Just so you know, I almost posted the first screenshot without the highlighting as a question, but then I saw the error in the screenshot, which made me post this post. (?)

Hopefully, none of you guys will experience something like this in your programming adventure.

Thanks for the small but powerful support,
Ben

Hi Ben!

Sometimes just stepping away from the problem can help you solve it! :slight_smile:

1 Like