'fetch last item of core data array

In my swift code below my goal is fetch the last image of the core data array. Right now my code is having a runtime error with indexpath. I dont know if you need to use a index path to do this. I have also added a screenshot that features all of the core data info.

pic of core data

var pic = UIImageView()
func loadImage(){
    if let imageData = itemNamez[IndexPath.last(pic)] {
        
        let coredataLoadedimage = UIImage(data: imageData)
        pic.image = coredataLoadedimage
        
    }
}


Solution 1:[1]

  • You can access array last index like I have done in code.

    let arr = ["a","b","c","d"]
    print(arr.last ?? "")
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sid_patel_1211