Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Custom CollectionViewCell Casting Failed when dequeueReusableCell

I got the error below when trying to cast the dequeueReusableCell to MenuCollectionViewCell which I have created a file for and changed the cell in the storyboard to that class. I am following this tutorial from raywenderlich. I have check the code with the example and couldn't find the mistake. What are common mistakes that cause this error?

If I change the casting back to as UICollectionViewCell then the error disappear.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as MenuCollectionViewCell


        cell.backgroundColor = UIColor.lightGrayColor()


        return cell
    }

Error:

libswiftCore.dylib`swift_dynamicCastClassUnconditional:
    0x10bd39860:  pushq  %rbp
    0x10bd39861:  movq   %rsp, %rbp
    0x10bd39864:  testq  %rdi, %rdi
    0x10bd39867:  je     0x10bd3989e               ; swift_dynamicCastClassUnconditional + 62
    0x10bd39869:  movabsq $-0x7fffffffffffffff, %rax
    0x10bd39873:  testq  %rax, %rdi
    0x10bd39876:  jne    0x10bd3989e               ; swift_dynamicCastClassUnconditional + 62
    0x10bd39878:  leaq   0xb52e9(%rip), %rax
    0x10bd3987f:  movq   (%rax), %rax
    0x10bd39882:  andq   (%rdi), %rax
    0x10bd39885:  nopw   %cs:(%rax,%rax)
    0x10bd39890:  cmpq   %rsi, %rax
    0x10bd39893:  je     0x10bd398ad               ; swift_dynamicCastClassUnconditional + 77
    0x10bd39895:  movq   0x8(%rax), %rax
    0x10bd39899:  testq  %rax, %rax
    0x10bd3989c:  jne    0x10bd39890               ; swift_dynamicCastClassUnconditional + 48
    0x10bd3989e:  leaq   0x36b7d(%rip), %rax       ; "Swift dynamic cast failed"
    0x10bd398a5:  movq   %rax, 0xb4c0c(%rip)       ; gCRAnnotations + 8
    0x10bd398ac:  int3   
    0x10bd398ad:  movq   %rdi, %rax
    0x10bd398b0:  popq   %rbp
    0x10bd398b1:  retq   
    0x10bd398b2:  nopw   %cs:(%rax,%rax)

Update: If tried checking the cell type with the code below and got false even though I have set it to MenuCollectionViewCell in the storyboard.

    print( cell is MenuCollectionViewCell ) => false

Storyboard cell setting

like image 220
harinsa Avatar asked Feb 04 '15 16:02

harinsa


1 Answers

The problem was the code below in viewDidLoad() method that came with the UICollectionViewController template. I removed it and everything worked as expected.

self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
like image 194
harinsa Avatar answered Oct 23 '22 08:10

harinsa