In my swift code the goal is 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
}
}