Accessing UserDefault Array URL to Populate CollectionView

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!