This is a follow-up from a previous post here.
I am now trying to access the array and use it to populate my collectionView. When I try the following code, my imageData comes back as nil and I get a crash, even though the imageURL correctly shows as a filepath.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let usD = UserDefaults.standard
let array = usD.stringArray(forKey: "WeatherArray") ?? []
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell
let myString = array[indexPath.row]
let imageUrl = URL(string: myString)!
let imageData = try? Data(contentsOf: imageUrl)
let image = UIImage(data: imageData!)
cell.myImageView.image = image
Here is an example of my URL(string: myString) below:
file:///private/var/mobile/Containers/Data/Application/6DD4ED20-F1BB-46DB-8A8C-35C3CFB2B2C6/tmp/01431A0E-F7C2-4A60-B1C6-5C9508C75863.jpeg
Also, inspecting imageUrl shows this error:
Printing description of imageUrl: expression produced error: error: /var/folders/np/4m9vk2yd0bzbf74lxrnffccr0000gn/T/expr9-6d45ad..swift:1:65: error: use of undeclared type 'Foundation' Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<Foundation.URL>(bitPattern: 0x1054f86a0)!.pointee)
tmp. You can't save a reference to a temporary file. If you are willing to load that file later you need to copy/move that file to a directory inside your app sandbox (documents directory)pathyou are saving itsabsoluteStringwhich it is not the same. It includes the url scheme which forces you to useURL(string:)initializer instead ofURL(fileURLWithPath:)