修改
添加异常断点后(感谢Daniel T),我发现它是我的
cell.askingUsernameLabel.text = NSString(string: arr[indexPath.row])
返回nil。
这个问题的解决方案是在swift中,注册Sub类会覆盖你在Storyboards中所做的事情(如果使用storyboard)。只需删除
self.collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"CollectionViewCell")
你很高兴
ORIGINAL
我真的迷失了这个:我试图从我的ViewController在UIView中设置一个UICollectionView。 (故事板)
我在几个教程/代码示例之后进行了设置,但是我无法找到为什么我最终总是会遇到"致命错误:在解开一个Optional值&#34时意外地发现了nil。
这是代码。
控制器层次结构:
View Controller (class:ViewController.swift)
-- UIView 1
-- UIView 2
----- UICollectionView
---------- UICollectionViewCell (class:CollectionViewCell.swift) (dataSource & delegate linked to View Contorller)
--------------- Label
ViewController.swift
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var arr:[String] = []
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
arr = ["title1", "title2"]
self.collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"CollectionViewCell")
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arr.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
cell.askingUsernameLabel.text = arr[indexPath.row]
return cell
}
}
CollectionViewCell
import UIKit
class CollectionViewCell: UICollectionViewCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBOutlet weak var askingUsernameLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
}
}
我最终遇到这个致命的错误我做错了什么?
there`@objc there.ViewController.collectionView (there.ViewController)(ObjectiveC.UICollectionView, cellForItemAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UICollectionViewCell at ViewController.swift:
0xb08c0: pushl %ebp
0xb08c1: movl %esp, %ebp
0xb08c3: pushl %esi
0xb08c4: subl $0x24, %esp
0xb08c7: movl 0x14(%ebp), %eax
0xb08ca: movl 0x10(%ebp), %ecx
0xb08cd: movl 0x8(%ebp), %edx
0xb08d0: movl %ecx, (%esp)
0xb08d3: movl %ecx, -0x8(%ebp)
0xb08d6: movl %edx, -0xc(%ebp)
0xb08d9: movl %eax, -0x10(%ebp)
0xb08dc: calll 0x13f0e0 ; symbol stub for: objc_retain
0xb08e1: movl -0x10(%ebp), %ecx
0xb08e4: movl %ecx, (%esp)
0xb08e7: movl %eax, -0x14(%ebp)
0xb08ea: calll 0x13f0e0 ; symbol stub for: objc_retain
0xb08ef: movl -0xc(%ebp), %ecx
0xb08f2: movl %ecx, (%esp)
0xb08f5: movl %eax, -0x18(%ebp)
0xb08f8: calll 0x13f0e0 ; symbol stub for: objc_retain
0xb08fd: movl -0x8(%ebp), %ecx
0xb0900: movl %ecx, (%esp)
0xb0903: movl -0x10(%ebp), %edx
0xb0906: movl %edx, 0x4(%esp)
0xb090a: movl -0xc(%ebp), %esi
0xb090d: movl %esi, 0x8(%esp)
0xb0911: movl %eax, -0x1c(%ebp)
0xb0914: calll 0xb0240 ; there.ViewController.collectionView (there.ViewController)(ObjectiveC.UICollectionView, cellForItemAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UICollectionViewCell at ViewController.swift:124
0xb0919: movl %esp, %ecx
0xb091b: movl %eax, (%ecx)
0xb091d: calll 0x13f06e ; symbol stub for: objc_autoreleaseReturnValue
0xb0922: addl $0x24, %esp
0xb0925: popl %esi
0xb0926: popl %ebp
0xb0927: retl
UIKit`-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]:
0x2730b98: pushl %ebp
0x2730b99: movl %esp, %ebp
0x2730b9b: pushl %ebx
0x2730b9c: pushl %edi
0x2730b9d: pushl %esi
0x2730b9e: subl $0x5c, %esp
0x2730ba1: calll 0x2730ba6 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 14
0x2730ba6: popl %esi
0x2730ba7: movl 0x8(%ebp), %edi
0x2730baa: movl 0x6307aa(%esi), %ebx
0x2730bb0: cmpl $0x0, (%edi,%ebx)
0x2730bb4: jne 0x2730c29 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 145
0x2730bb6: movl 0x6276ba(%esi), %eax
0x2730bbc: movl 0x611b46(%esi), %ecx
0x2730bc2: movl %ecx, 0x4(%esp)
0x2730bc6: movl %eax, (%esp)
0x2730bc9: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730bce: movl %eax, -0x34(%ebp)
0x2730bd1: movl 0x6276be(%esi), %eax
0x2730bd7: movl 0x611b4a(%esi), %ecx
0x2730bdd: leal 0x31216a(%esi), %edx
0x2730be3: movl %edx, 0x8(%esp)
0x2730be7: movl %ecx, 0x4(%esp)
0x2730beb: movl %eax, (%esp)
0x2730bee: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730bf3: movl 0x611b4e(%esi), %ecx
0x2730bf9: leal 0x519532(%esi), %edx
0x2730bff: movl %edx, 0x18(%esp)
0x2730c03: movl %eax, 0x10(%esp)
0x2730c07: movl %edi, 0xc(%esp)
0x2730c0b: movl 0xc(%ebp), %eax
0x2730c0e: movl %eax, 0x8(%esp)
0x2730c12: movl %ecx, 0x4(%esp)
0x2730c16: movl -0x34(%ebp), %eax
0x2730c19: movl %eax, (%esp)
0x2730c1c: movl $0x51c, 0x14(%esp)
0x2730c24: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730c29: movl 0x10(%ebp), %edx
0x2730c2c: movl 0x63075a(%esi), %eax
0x2730c32: movl (%edi,%eax), %eax
0x2730c35: testl %eax, %eax
0x2730c37: je 0x2730c7d ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 229
0x2730c39: movl 0x6307b6(%esi), %ecx
0x2730c3f: cmpl $0x1, (%edi,%ecx)
0x2730c43: jne 0x2730c7d ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 229
0x2730c45: movl 0x611e16(%esi), %ecx
0x2730c4b: movl %edx, 0x8(%esp)
0x2730c4f: movl %ecx, 0x4(%esp)
0x2730c53: movl %eax, (%esp)
0x2730c56: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730c5b: movl 0x10(%ebp), %edx
0x2730c5e: testb %al, %al
0x2730c60: je 0x2730c7d ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 229
0x2730c62: movl 0x630752(%esi), %eax
0x2730c68: movl (%edi,%eax), %eax
0x2730c6b: movl 0x611a4e(%esi), %ecx
0x2730c71: movl %ecx, 0x4(%esp)
0x2730c75: movl %eax, (%esp)
0x2730c78: jmp 0x273100d ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 1141
0x2730c7d: movl (%edi,%ebx), %eax
0x2730c80: movl 0x62428a(%esi), %ecx
0x2730c86: movl %edx, 0xc(%esp)
0x2730c8a: movl %edi, 0x8(%esp)
0x2730c8e: movl %ecx, 0x4(%esp)
0x2730c92: movl %eax, (%esp)
0x2730c95: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730c9a: movl %eax, %ebx
0x2730c9c: testl %ebx, %ebx
0x2730c9e: jne 0x2730d19 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 385
0x2730ca0: movl 0x6276ba(%esi), %eax
0x2730ca6: movl 0x611b46(%esi), %ecx
0x2730cac: movl %ecx, 0x4(%esp)
0x2730cb0: movl %eax, (%esp)
0x2730cb3: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730cb8: movl %eax, %edi
0x2730cba: movl 0x6276be(%esi), %eax
0x2730cc0: movl 0x611b4a(%esi), %ecx
0x2730cc6: leal 0x31216a(%esi), %edx
0x2730ccc: movl %edx, 0x8(%esp)
0x2730cd0: movl %ecx, 0x4(%esp)
0x2730cd4: movl %eax, (%esp)
0x2730cd7: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730cdc: movl 0x611b4e(%esi), %ecx
0x2730ce2: movl 0x10(%ebp), %edx
0x2730ce5: movl %edx, 0x1c(%esp)
0x2730ce9: leal 0x519542(%esi), %edx
0x2730cef: movl %edx, 0x18(%esp)
0x2730cf3: movl %eax, 0x10(%esp)
0x2730cf7: movl 0x8(%ebp), %eax
0x2730cfa: movl %eax, 0xc(%esp)
0x2730cfe: movl 0xc(%ebp), %eax
0x2730d01: movl %eax, 0x8(%esp)
0x2730d05: movl %ecx, 0x4(%esp)
0x2730d09: movl %edi, (%esp)
0x2730d0c: movl $0x522, 0x14(%esp)
0x2730d14: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730d19: movl 0x6154de(%esi), %eax
0x2730d1f: movl %eax, 0x4(%esp)
0x2730d23: movl %ebx, (%esp)
0x2730d26: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730d2b: testl %eax, %eax
0x2730d2d: jne 0x2730da1 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 521
0x2730d2f: movl 0x6276ba(%esi), %eax
0x2730d35: movl 0x611b46(%esi), %ecx
0x2730d3b: movl %ecx, 0x4(%esp)
0x2730d3f: movl %eax, (%esp)
0x2730d42: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730d47: movl %eax, %edi
0x2730d49: movl 0x6276be(%esi), %eax
0x2730d4f: movl 0x611b4a(%esi), %ecx
0x2730d55: leal 0x31216a(%esi), %edx
0x2730d5b: movl %edx, 0x8(%esp)
0x2730d5f: movl %ecx, 0x4(%esp)
0x2730d63: movl %eax, (%esp)
0x2730d66: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730d6b: movl 0x611b4e(%esi), %ecx
0x2730d71: leal 0x519552(%esi), %edx
0x2730d77: movl %edx, 0x18(%esp)
0x2730d7b: movl %eax, 0x10(%esp)
0x2730d7f: movl 0x8(%ebp), %eax
0x2730d82: movl %eax, 0xc(%esp)
0x2730d86: movl 0xc(%ebp), %eax
0x2730d89: movl %eax, 0x8(%esp)
0x2730d8d: movl %ecx, 0x4(%esp)
0x2730d91: movl %edi, (%esp)
0x2730d94: movl $0x523, 0x14(%esp)
0x2730d9c: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730da1: movl %ebx, %edi
0x2730da3: movl 0x4d2486(%esi), %eax
0x2730da9: movl (%eax), %eax
0x2730dab: testl %eax, %eax
0x2730dad: je 0x273101a ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 1154
0x2730db3: cmpl $0x6ffff, %eax
0x2730db8: seta %bl
0x2730dbb: movl 0x62428e(%esi), %eax
0x2730dc1: movl %eax, 0x4(%esp)
0x2730dc5: movl %edi, (%esp)
0x2730dc8: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730dcd: testb %bl, %bl
0x2730dcf: je 0x2730e64 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 716
0x2730dd5: testb %al, %al
0x2730dd7: movl %edi, %ebx
0x2730dd9: movl 0x14(%ebp), %ecx
0x2730ddc: jne 0x2730e8a ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 754
0x2730de2: movl 0x6276ba(%esi), %eax
0x2730de8: movl 0x611b46(%esi), %ecx
0x2730dee: movl %ecx, 0x4(%esp)
0x2730df2: movl %eax, (%esp)
0x2730df5: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730dfa: movl %eax, %edi
0x2730dfc: movl 0x6276be(%esi), %eax
0x2730e02: movl 0x611b4a(%esi), %ecx
0x2730e08: leal 0x31216a(%esi), %edx
0x2730e0e: movl %edx, 0x8(%esp)
0x2730e12: movl %ecx, 0x4(%esp)
0x2730e16: movl %eax, (%esp)
0x2730e19: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730e1e: movl 0x611b4e(%esi), %ecx
0x2730e24: movl %ebx, 0x20(%esp)
0x2730e28: movl 0x10(%ebp), %edx
0x2730e2b: movl %edx, 0x1c(%esp)
0x2730e2f: leal 0x519562(%esi), %edx
0x2730e35: movl %edx, 0x18(%esp)
0x2730e39: movl %eax, 0x10(%esp)
0x2730e3d: movl 0x8(%ebp), %eax
0x2730e40: movl %eax, 0xc(%esp)
0x2730e44: movl 0xc(%ebp), %eax
0x2730e47: movl %eax, 0x8(%esp)
0x2730e4b: movl %ecx, 0x4(%esp)
0x2730e4f: movl %edi, (%esp)
0x2730e52: movl $0x524, 0x14(%esp)
0x2730e5a: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730e5f: movl 0x14(%ebp), %ecx
0x2730e62: jmp 0x2730e8a ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 754
0x2730e64: testb %al, %al
0x2730e66: movl %edi, %ebx
0x2730e68: movl 0x14(%ebp), %ecx
0x2730e6b: jne 0x2730e8a ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 754
0x2730e6d: movl %ebx, 0x8(%esp)
0x2730e71: movl 0x10(%ebp), %eax
0x2730e74: movl %eax, 0x4(%esp)
0x2730e78: leal 0x519562(%esi), %eax
0x2730e7e: movl %eax, (%esp)
0x2730e81: movl %ecx, %edi
0x2730e83: calll 0x29148fa ; symbol stub for: NSLog
0x2730e88: movl %edi, %ecx
0x2730e8a: movl %ebx, -0x34(%ebp)
0x2730e8d: movl 0x624292(%esi), %eax
0x2730e93: movl %ecx, 0x8(%esp)
0x2730e97: movl %eax, 0x4(%esp)
0x2730e9b: movl %ebx, (%esp)
0x2730e9e: movl %ecx, %edi
0x2730ea0: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730ea5: movl %eax, %ebx
0x2730ea7: movl 0x611e16(%esi), %eax
0x2730ead: movl %edi, 0x8(%esp)
0x2730eb1: movl %eax, 0x4(%esp)
0x2730eb5: movl %ebx, (%esp)
0x2730eb8: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730ebd: testb %al, %al
0x2730ebf: jne 0x2730f29 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 913
0x2730ec1: movl 0x63072e(%esi), %ecx
0x2730ec7: movl %ecx, -0x38(%ebp)
0x2730eca: movl 0x8(%ebp), %eax
0x2730ecd: movl (%eax,%ecx), %eax
0x2730ed0: movl 0x61dfba(%esi), %ecx
0x2730ed6: movl %edi, 0xc(%esp)
0x2730eda: movl %ebx, 0x8(%esp)
0x2730ede: movl %ecx, 0x4(%esp)
0x2730ee2: movl %eax, (%esp)
0x2730ee5: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730eea: testb %al, %al
0x2730eec: je 0x2730f29 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 913
0x2730eee: movl 0x8(%ebp), %eax
0x2730ef1: movl -0x38(%ebp), %ecx
0x2730ef4: movl (%eax,%ecx), %ecx
0x2730ef7: movl 0x61e006(%esi), %eax
0x2730efd: movl %edi, 0xc(%esp)
0x2730f01: movl %ebx, 0x8(%esp)
0x2730f05: movl %eax, 0x4(%esp)
0x2730f09: movl %ecx, (%esp)
0x2730f0c: movl %ecx, %ebx
0x2730f0e: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730f13: movl 0x61dfe2(%esi), %ecx
0x2730f19: movl %eax, 0x8(%esp)
0x2730f1d: movl %ecx, 0x4(%esp)
0x2730f21: movl %ebx, (%esp)
0x2730f24: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730f29: movl %edi, %edx
0x2730f2b: cmpb $0x0, 0x18(%ebp)
0x2730f2f: movl 0x8(%ebp), %edi
0x2730f32: movl -0x34(%ebp), %ebx
0x2730f35: je 0x2730f88 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 1008
0x2730f37: movl 0x6276d2(%esi), %eax
0x2730f3d: movl 0x4d246e(%esi), %ecx
0x2730f43: movl %ecx, -0x30(%ebp)
0x2730f46: movl $0xc2000000, -0x2c(%ebp)
0x2730f4d: movl $0x0, -0x28(%ebp)
0x2730f54: leal 0x487(%esi), %ecx
0x2730f5a: movl %ecx, -0x24(%ebp)
0x2730f5d: leal 0x4f864a(%esi), %ecx
0x2730f63: movl %ecx, -0x20(%ebp)
0x2730f66: movl %edi, -0x1c(%ebp)
0x2730f69: movl %edx, -0x18(%ebp)
0x2730f6c: movl %ebx, -0x14(%ebp)
0x2730f6f: movl 0x612d42(%esi), %ecx
0x2730f75: leal -0x30(%ebp), %edx
0x2730f78: movl %edx, 0x8(%esp)
0x2730f7c: movl %ecx, 0x4(%esp)
0x2730f80: movl %eax, (%esp)
0x2730f83: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730f88: movl 0x62424a(%esi), %eax
0x2730f8e: movl %eax, 0x4(%esp)
0x2730f92: movl %ebx, (%esp)
0x2730f95: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730f9a: movl 0x624272(%esi), %ecx
0x2730fa0: movl %ecx, 0x4(%esp)
0x2730fa4: movl %eax, (%esp)
0x2730fa7: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730fac: movl 0x62429a(%esi), %ecx
0x2730fb2: movl %eax, 0xc(%esp)
0x2730fb6: movl %ebx, 0x8(%esp)
0x2730fba: movl %ecx, 0x4(%esp)
0x2730fbe: movl %edi, (%esp)
0x2730fc1: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2730fc6: movl 0x630732(%esi), %eax
0x2730fcc: movl (%edi,%eax), %eax
0x2730fcf: testb $0x1, %ah
0x2730fd2: je 0x2731000 ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 1128
0x2730fd4: movl 0x4d277e(%esi), %eax
0x2730fda: movl (%eax), %eax
0x2730fdc: movl (%edi,%eax), %eax
0x2730fdf: movl 0x6241ee(%esi), %ecx
0x2730fe5: movl 0x10(%ebp), %edx
0x2730fe8: movl %edx, 0x10(%esp)
0x2730fec: movl %ebx, 0xc(%esp)
0x2730ff0: movl %edi, 0x8(%esp)
0x2730ff4: movl %ecx, 0x4(%esp)
0x2730ff8: movl %eax, (%esp)
0x2730ffb: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2731000: movl 0x611a4e(%esi), %eax
0x2731006: movl %eax, 0x4(%esp)
0x273100a: movl %ebx, (%esp)
0x273100d: calll 0x2914ab6 ; symbol stub for: objc_msgSend
0x2731012: addl $0x5c, %esp
0x2731015: popl %esi
0x2731016: popl %edi
0x2731017: popl %ebx
0x2731018: popl %ebp
0x2731019: retl
0x273101a: movl $0x70000, (%esp)
0x2731021: calll 0x2017c4b ; _UIApplicationLinkedOnOrAfter
0x2731026: movb %al, %bl
0x2731028: jmp 0x2730dbb ; -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 547
答案 0 :(得分:0)
在代码中加入一些断点。逐步执行每个函数并查看变量包含的值。找不到它的nil变量。
如果错误实际出现在您提供的代码中,则错误可能出现在cell.askingUsernameLabel.text = arr[indexPath.row]
行或self.collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"CollectionViewCell")
行。
你知道怎么放入异常断点吗?以下是: