-1

I want to save an item in the second spot of a binary data core data attribute. The core data entity is named Pictucre and the attribute is named pic. My code right is based off of something that would append the item to the core data attribute. What I want to do is is save it at slot 2. If something is at slot 2 it just saves over it. I am getting a error at

CoredataHandler.shareInstance.saveImage(data: imageData1)

Value of type 'UIImage' has no subscripts

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

  @objc func leftM(){
     if let imageData = imagePlace.image {
                CoredataHandler.shareInstance.saveImage(data: imageData[1])
            }
 }
        
 class CoredataHandler : NSManagedObject {
        static let shareInstance = CoredataHandler()
  func saveImage(data: Data) {
            let imageInstance = Pictucre(context: context)
            
            
            imageInstance.pic = data
                
            do {
                try context.save()
              
            } catch {
                print(error.localizedDescription)
            }
        }

 class CoredataHandler : NSManagedObject {
        static let shareInstance = CoredataHandler()
  func saveImage(data: Data) {
            let imageInstance = Pictucre(context: context)
            
            
            imageInstance.pic = data
                
            do {
                try context.save()
              
            } catch {
                print(error.localizedDescription)
            }
        }}

pic

1 Answers1

0

What these errors are telling you is:

  1. imageData is a UIImage
  2. You are attempting to use a subscript (here, [1]) on imageData.
  3. UIImage does not use subscripts, so this line makes no sense.

Numeric subscripts are used on arrays or other collections to look up the item at a specific image. If you had an array of images, you could use [1] to access the image at index 1. A single UIImage is not any kind of collection, it’s just one image, so using a numeric subscript doesn’t make sense.

It’s hard to tell what you’re trying to do—- ” the second spot of a binary data core data attribute” doesn’t really make sense, because binary attributes don’t have multiple “spots”, they just hold a bunch of bytes that could represent an image, or really anything you want. A numeric index doesn’t make sense, because there is no concept of indexing or “spots” on binary attributes.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • I added a photo above that hopefully helps. In entity pic slot 0,1,2 are filled. The func @objc func leftM(){} should save the image view imageplace image on slot 1 assume there is already an image in slot 1 and the func is overwriting the current image. Thanks. – John Zalubski Dec 19 '22 at 01:06
  • I see your picture but it just shows four squares. Binary attributes still do not have slots so the image does not reflect how binary attributes work. Where do you think these ‘slots’ come from? Do you have an array somewhere that you haven’t mentioned? – Tom Harrington Dec 19 '22 at 16:40
  • i do not have another array the slots represent the order in which pic is saved. There is a saved item in slot 0,1,2 but not in slot 3. – John Zalubski Dec 20 '22 at 14:22
  • You keep mentioning ‘slots’. There is no such thing in a binary attribute or in a `UIImage`. What, exactly and specifically, are you doing when you access what you call a slot? Because you’re definitely not doing it on an anything like what you have described in this question. – Tom Harrington Dec 20 '22 at 16:33
  • in the array of pic when it is fetched when i am trying to fetch the 1st item in pic i want something to come up. I want it to be saved connected to 1. – John Zalubski Dec 20 '22 at 16:54
  • Your code does not show any arrays. Using array-style indexing on a `UIImage` doesn't make it an array. – Tom Harrington Dec 20 '22 at 18:35
  • What do you recommend I do to save at position 1? – John Zalubski Dec 20 '22 at 18:42
  • @JohnZalubski I honestly don't know what you mean when you say "position 1". Position 1 of what? If you had an array that might make sense. "Position 1" of a `UIImage` is not something that happens, because `UIImage` does not have positions. – Tom Harrington Dec 20 '22 at 23:32
  • I guess, if you want multiple positions, use an array? That's what they're for. – Tom Harrington Dec 21 '22 at 00:14