In my swift code below the goal is to fetch a series of Boolean values. It should fetch multiple values. Something along the lines of false, false, true etc. Right now I am having issues in my base class on if let imageData = info[imageNo].bool causing a runtime error stating .bool is not a optional value. In my core data database the entity is called OnOff and the attribute is bool which a Boolean value. How can I fix my code to fetch the boolean values?
Base Class
class ViewController: UIViewController {
func getBool(imageNo:Int) {
// first check the array bounds
let info = helpBool.shareInstance.fetchBool()
print("image count is", info.count)
if info.count > imageNo {
// check if the data is available
if let imageData = info[imageNo].bool
{
print(imageData)
} else {
// no data
print("data is empty Textss")
}
} else {
// image number is greater than array bounds
print("you are asking out of bounds")
}
}
Helper Class
class helpBool: UIViewController{
static let shareInstance = helpBool()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
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
}
}