斯威夫特4: 我得到一個錯誤約UICollectionView崩潰下載時圖像
「意外發現零而展開的可選值」
和斷點是我downloadImage
而我從JSON查詢中填入UICollectionView
。
有關我如何使這更安全的任何建議?還是我看錯了東西? 這裏是downloadImage代碼:
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()
}
這裏是函數填充的CollectionView它打破:
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
//這裏崩潰
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
}
這裏是控制檯輸出:
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)
這裏是調試:
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)
這裏是你們在詢問該URL的控制檯輸出 - 它看起來像一個類別被點擊時,它的第一類加載的圖像確定,但隨後發現零。
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)
我該如何優雅地接受函數中的這個nil值?
可以粘貼你的崩潰日誌? –
你的網址在這行中必須爲「self.articles?[indexPath.item] .imageURL!」。或者在這裏「URL(string:url)!」 。請檢查您的網址 –
您可以調試以檢查此結果(self.articles?[indexPath.item] .imageURL!)!每次當你下載圖像。我認爲有一個URL –