Fetch core data string by int in a parameter function

In my swift code I attempted to fetch core data string using a int. I have 2 classes my base class and my helper class. In my base class that is where the func attempting to fetch the string. That func has a error message at fetchImageTxt.text = String(data: imageData) stating Missing argument for parameter ‘encoding’ in call. Don’t know how to resolve it.


     class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
      var fetchImageTxt = UITextField()

func getT(imageNo:Int) {
   // first check the array bounds
   let info = DataBaseHelper.shareInstance.fetchImage()
   if info.count > imageNo {
       // check if the data is available
       if let imageData = info[imageNo].img {
           fetchImageTxt.text = String(data: imageData)
       } else {
           // no data
           print("data is empty")
   } else {
       // image number is greater than array bounds
       print("you are asking out of bounds")


class DataBaseHelper {

static let shareInstance = DataBaseHelper()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func fetchImage() -> [Info] {
    var fetchingImage = [Info]()
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Info")
    do {
        fetchingImage = try context.fetch(fetchRequest) as! [Info]
    } catch {
        print("Error while fetching the image")
    return fetchingImage

Screen Shot 2022-08-14 at 10.00.48 AM

I have a lot of questions about what exactly you are trying to do. However, I can answer specifically about the encoding.

When you initialize a String with data, it is essentially a binary blob that will be converted into characters. The thing is that those characters can be encoded in many formats (most typically UTF-8, UTF-16, ASCII, etc). While the framework can do some educated guessing (for example UTF-8 sometimes contains a special sequence called BOM at the start of the data), it’s best if you supply such value.

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