问题
After using the Swift 3 converter on my project, I keep crashing when loading a subclass of a UICollectionViewController. Below is my code with the relevant methods for the collection view.
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photos.count
}
override func collectionView(_ collectionView: UICollectionView, c`enter code here`ellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: tileReuseIdentifier, for: indexPath) as! TileCollectionViewCell
cell.captionLabel.text = photos[(indexPath as NSIndexPath).item].caption
cell.captionLabel.adjustsFontSizeToFitWidth = true
cell.imageView.image = photos[(indexPath as NSIndexPath).item].image
cell.layer.cornerRadius = 10
cell.layer.shadowPath = UIBezierPath(roundedRect: cell.frame, cornerRadius: cell.layer.cornerRadius).cgPath
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: cellSize, height: cellSize)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex indexPath: IndexPath) -> CGFloat {
return spacing
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerReuseIdentifier, for: indexPath) as! HeaderCollectionReusableView
reusableView.setupView()
return reusableView
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: cellSize-20)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(topMargin, spacing, bottomMargin, spacing)
}
And this is the stack trace when it crashes:
libswiftFoundation.dylib`static Foundation.IndexPath._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSIndexPath>) -> Foundation.IndexPath:
0x100a549e0 <+0>: stp x20, x19, [sp, #-32]!
0x100a549e4 <+4>: stp x29, x30, [sp, #16]
0x100a549e8 <+8>: add x29, sp, #16 ; =16
0x100a549ec <+12>: mov x19, x0
0x100a549f0 <+16>: cbz x19, 0x100a54a18 ; <+56>
0x100a549f4 <+20>: mov x0, x19
0x100a549f8 <+24>: bl 0x100a7cfd4 ; function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Dead> of Foundation.IndexPath.init (nsIndexPath : __ObjC.NSIndexPath) -> Foundation.IndexPath
0x100a549fc <+28>: mov x20, x0
0x100a54a00 <+32>: mov x0, x19
0x100a54a04 <+36>: bl 0x100abcfe4 ; symbol stub for: objc_release
0x100a54a08 <+40>: mov x0, x20
0x100a54a0c <+44>: ldp x29, x30, [sp, #16]
0x100a54a10 <+48>: ldp x20, x19, [sp], #32
0x100a54a14 <+52>: ret
-> 0x100a54a18 <+56>: brk #0x1
Some searching led me to https://bugs.swift.org/browse/SR-2103 and https://bugs.swift.org/browse/SR-2417
What's strange is that when I set breakpoints at the beginning of the above methods, the app crashes before the any of the breakpoints are reached. viewDidLoad executes and returns. Then the app crashes with the above error.
Can anybody help?
来源:https://stackoverflow.com/questions/39621534/uicollectionview-broken-after-swift-3-migration