UICollectionView Crashes when downloading images

时间:2017-12-18 05:51:42

标签: ios arrays swift

Swift 4: I'm getting an error about

'Unexpectedly found nil while unwrapping an Optional value'

and the breakpoint is my downloadImage while I am filling a UICollectionView from a JSON query.

Any suggestions on how I can make this safer? Or Am I looking at the wrong thing? Here is the downloadImage code:

extension UIImageView {
    @objc func downloadImage(from url: String){
        let urlRequest = URLRequest(url: URL(string: url)!)
        let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in

            if error != nil {
                print(error as Any)
                return
            }
            DispatchQueue.main.async {
                if UIImage(data: data!) != nil {
                self.image = UIImage(data: data!)
                }
            }

        }
        task.resume()

    }

And here is the function filling the collectionView where it breaks:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "articleCell", for: indexPath) as! ArticleCell

        cell.title.text = self.articles?[indexPath.item].headline

//Crashes here

cell.imgView.downloadImage(from: (self.articles?[indexPath.item].imageURL!)!)

    cell.contentView.layer.cornerRadius = 0.5
    cell.contentView.layer.borderWidth = 1.0

    cell.contentView.layer.borderColor = UIColor.clear.cgColor
    cell.contentView.layer.masksToBounds = true

    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 0.5)
    cell.layer.shadowRadius = 1
    cell.layer.shadowOpacity = 0.5
    cell.layer.masksToBounds = false
    cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).cgPath


    return cell


}

Here is the console output:

2017-12-18 06:55:08.412546+0100 Keyboard[81355:4788284] Failed to inherit CoreMedia permissions from 81260: (null)
2017-12-18 06:55:08.634498+0100 Keyboard[81355:4788188] [MC] Lazy loading NSBundle MobileCoreServices.framework
2017-12-18 06:55:08.635348+0100 Keyboard[81355:4788188] [MC] Loaded MobileCoreServices.framework
2017-12-18 06:55:08.637140+0100 Keyboard[81355:4788188] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/me/Library/Developer/CoreSimulator/Devices/9BAC6166-3559-4962-9715-F388A162E95F/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
Category: business
true
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2017-12-18 06:55:11.138236+0100 Keyboard[81355:4788188] Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) 

And here is the debug:

libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage(Swift.StaticString, Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never:
    0x113a5e420 <+0>:  pushq  %rbp
    0x113a5e421 <+1>:  movq   %rsp, %rbp
    0x113a5e424 <+4>:  pushq  %r15
    0x113a5e426 <+6>:  pushq  %r14
    0x113a5e428 <+8>:  pushq  %r13
    0x113a5e42a <+10>: pushq  %r12
    0x113a5e42c <+12>: pushq  %rbx
    0x113a5e42d <+13>: pushq  %rax
    0x113a5e42e <+14>: movl   %r9d, %r14d
    0x113a5e431 <+17>: movq   %r8, %r15
    0x113a5e434 <+20>: movq   %rcx, %rbx
    0x113a5e437 <+23>: movl   %edx, -0x2c(%rbp)
    0x113a5e43a <+26>: movq   %rsi, %r13
    0x113a5e43d <+29>: movq   %rdi, %r12
    0x113a5e440 <+32>: leaq   0x1c6551(%rip), %rdi      ; protocol descriptor for Swift._DefaultCustomPlaygroundQuickLookable + 136
    0x113a5e447 <+39>: movl   $0x28, %esi
    0x113a5e44c <+44>: movl   $0x7, %edx
    0x113a5e451 <+49>: callq  0x113a826b0               ; swift_rt_swift_allocObject
    0x113a5e456 <+54>: movq   %rbx, 0x10(%rax)
    0x113a5e45a <+58>: movq   %r15, 0x18(%rax)
    0x113a5e45e <+62>: movb   %r14b, 0x20(%rax)
    0x113a5e462 <+66>: movl   0x10(%rbp), %ecx
    0x113a5e465 <+69>: movl   %ecx, 0x24(%rax)
    0x113a5e468 <+72>: leaq   0xd4c81(%rip), %rcx       ; partial apply forwarder for closure #2 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._fatalErrorMessage(Swift.StaticString, Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never
    0x113a5e46f <+79>: movq   %r12, %rdi
    0x113a5e472 <+82>: movq   %r13, %rsi
    0x113a5e475 <+85>: movl   -0x2c(%rbp), %edx
    0x113a5e478 <+88>: movq   %rax, %r8
    0x113a5e47b <+91>: callq  0x1138aa1b0               ; function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer<A>((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A
->  0x113a5e480 <+96>: ud2    
    0x113a5e482 <+98>: nopw   %cs:(%rax,%rax)

And here is the console output from that URL you guys were asking about - it looks like the first category loads the images ok, but then when the next category is clicked it finds nil.

Optional("http://www.afr.com/content/dam/images/h/0/6/g/p/6/image.related.socialLead.620x349.h06bfz.png/1513570709452.jpg")
Optional("http://www.afr.com/content/dam/images/h/0/6/b/z/i/image.related.afrArticleLead.620x365.h06aej.png/1513559758977.jpg")
Optional("http://www.afr.com/content/dam/images/h/0/6/f/x/o/image.related.afrArticleLead.620x365.h06bh0.png/1513563680637.jpg")
Optional("http://assets1.ignimgs.com/2017/12/15/15234538txuhe2cy-1513377967388_1280w.jpg")
Optional("http://assets1.ignimgs.com/2017/12/18/the-last-jedi-2-1513558653477_1280w.jpg")
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2017-12-18 07:07:03.222266+0100 Keyboard[87661:4872775] Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) 

How can I gracefully accept this nil value in the function?

1 个答案:

答案 0 :(得分:2)

Try below code and always try to avoid forced unwrapping(!).

extension UIImageView {

@objc func downloadImage(from url: String){

    if let urlconvert  = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),let urlConvertible = URL(string:urlconvert){

        let urlRequest = URLRequest(url: urlConvertible)
        let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in

            if error != nil {
                print(error as Any)
                return
            }
            DispatchQueue.main.async {
                if let data = data {

                    self.image = UIImage(data: data)
                }
            }

        }
        task.resume()

    }
  }

}

change this one

cell.imgView.downloadImage(from: (self.articles?[indexPath.item].imageURL!)!)

to:

if let imageURL =  self.articles?[indexPath.item].imageURL{

cell.imgView.downloadImage(from: imageURL)
}