Home > Software engineering >  swift code not fetching a false boolean value
swift code not fetching a false boolean value

Time:09-12

in my swift codes the Goal is it to fetch a false boolean value. Right now my code fetches a true boolean value but it does not fetch a false one. In my debug section the only thing that is printed is "it is inside true" it should also print "it is inside false" but it is not printing it. " helpBool.shareInstance.saveBoolean(false)" is attempting to save the boolean value as false but it may not be the right way to do it.

    import UIKit;import CoreData

class ViewController: UIViewController {
    
    
    
    func getBool(imageNo:Int) {
       // first check the array bounds
       let info = helpBool.shareInstance.fetchBool()
       if info.count > imageNo {
           
     
           
           if info[imageNo].bool {
               
            
               
               if info[imageNo].bool == true {

                   print("it is inside true")
               }
               
             
               if info[imageNo].bool == false {
                   print("it is inside false")
               }
               

           }
       
               
               
               
           }
           //

    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        helpBool.shareInstance.saveBoolean(true)
         
         helpBool.shareInstance.saveBoolean(false)
        
        getBool(imageNo: 0)
        getBool(imageNo: 1)
        
    }


}


class helpBool: UIViewController{
    
    static let shareInstance = helpBool()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
 
    
    func saveBoolean(_ boolean: Bool) {
        let imageInstance = OnOff(context: context)
        imageInstance.bool = boolean
        
        do {
            try context.save()

        } catch {
            print(error.localizedDescription)
        }
        
    }
    
    
    

    func fetchBool() -> [OnOff] {
        
      
        var fetchingImage = [OnOff]()
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "OnOff")
        
        do {
            
            fetchingImage = try context.fetch(fetchRequest) as! [OnOff]
        } catch {
            print("Error while fetching the image")
        }
        
        return fetchingImage
    }
    
    
   
}

CodePudding user response:

You have an outer-most if block which only checks if info[imageNo].bool is true, so you will never get inside of it in the case where the bool is false, therefore the other check if info[imageNo].bool == false will never be true and your print statement will never be executed. You have to remove the outer-most if statement:

func getBool(imageNo:Int) {
       // first check the array bounds
       let info = helpBool.shareInstance.fetchBool()
       if info.count > imageNo {
           // if info[imageNo].bool {
               if info[imageNo].bool == true {
                   print("it is inside true")
               }
             
               if info[imageNo].bool == false {
                   print("it is inside false")
               }
        // }   
        }
    }

EDIT:

You could write your method a little bit cleaner like this (thanks @jnpdx and @Soumya Mahunt for the suggestions):

func getBool(imageNo: Int) {
    // first check the array bounds
    let info = helpBool.shareInstance.fetchBool()
    guard info.count > imageNo else { return }
    
    if info[imageNo].bool {
       print("it is inside true")   
       return
    }
    
    print("it is inside false")   
}

I strongly suggest checking out this well-accepted Swift style guide.

  • Related