# Fetch Boolean values from core data

**URL:** <https://forums.kodeco.com/t/fetch-boolean-values-from-core-data/177172>\
**Category:** Swift\
**Created:** [August 23, 2022, 1:19am UTC](https://forums.kodeco.com/t/fetch-boolean-values-from-core-data/177172 "2022-08-23T01:19:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 23, 2022, 1:20am UTC](https://forums.kodeco.com/t/fetch-boolean-values-from-core-data/177172/1 "2022-08-23T01:20:00Z")

</div>

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

```auto
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

```auto
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
}
}

```

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [August 23, 2022, 7:08pm UTC](https://forums.kodeco.com/t/fetch-boolean-values-from-core-data/177172/2 "2022-08-23T19:08:34Z")

</div>

Could you define _OnOff_?

If _OnOff.bool_ is just a regular Bool, then there’s no need to conditionally assign the value:

```swift
if let imageData = info[imageNo].bool

```

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [February 5, 2023, 5:20pm UTC](https://forums.kodeco.com/t/fetch-boolean-values-from-core-data/177172/3 "2023-02-05T17:20:03Z")

</div>

Closed on February 5, 2023, 5:20pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
