1

Does anyone know why I am getting the exception at this code block:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("ListCell", forIndexPath: indexPath) as ListTableViewCell
    let item = fetchedResultController.objectAtIndexPath(indexPath) as List
    cell.list_name?.text = item.list_name
    return cell
}

Here is the custom class I am casting to:

   import UIKit
   import CoreData

  class List: NSManagedObject {
      @NSManaged var list_name: String
      @NSManaged var items: NSSet
  }

The exception happens at this line:

    let item = fetchedResultController.objectAtIndexPath(indexPath) as List

Here is the exception:

`libswiftCore.dylib`swift_dynamicCastClassUnconditional:
0x10d630860:  pushq  %rbp
0x10d630861:  movq   %rsp, %rbp
0x10d630864:  testq  %rdi, %rdi
0x10d630867:  je     0x10d63089e               ; swift_dynamicCastClassUnconditional + 62
0x10d630869:  movabsq $-0x7fffffffffffffff, %rax
0x10d630873:  testq  %rax, %rdi
0x10d630876:  jne    0x10d63089e               ; swift_dynamicCastClassUnconditional + 62
0x10d630878:  leaq   0xb52e9(%rip), %rax
0x10d63087f:  movq   (%rax), %rax
0x10d630882:  andq   (%rdi), %rax
0x10d630885:  nopw   %cs:(%rax,%rax)
0x10d630890:  cmpq   %rsi, %rax
0x10d630893:  je     0x10d6308ad               ; swift_dynamicCastClassUnconditional + 77
0x10d630895:  movq   0x8(%rax), %rax
0x10d630899:  testq  %rax, %rax
0x10d63089c:  jne    0x10d630890               ; swift_dynamicCastClassUnconditional + 48
0x10d63089e:  leaq   0x36b7d(%rip), %rax       ; "Swift dynamic cast failed"
0x10d6308a5:  movq   %rax, 0xb4c0c(%rip)       ; gCRAnnotations + 8
0x10d6308ac:  int3   
0x10d6308ad:  movq   %rdi, %rax
0x10d6308b0:  popq   %rbp
0x10d6308b1:  retq   
0x10d6308b2:  nopw   %cs:(%rax,%rax)`
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
J0NNY ZER0
  • 707
  • 2
  • 13
  • 32

2 Answers2

3

The object being returned by objectAtIndexPath(_) isn't a List.

You should always use conditional casting if it's not guaranteed to be List, like so:

if let item = fetchedResultController.objectAtIndexPath(indexPath) as? List {
    // item is a List
    cell.list_name?.text = item.list_name
} else {
    // item isn't a List
    // See what it is if you're not expecting this
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • My List is a custom class. objectAtIndexPath(_) can be AnyObject. Shouldn't that allow it to be able to handle my List – J0NNY ZER0 Mar 22 '15 at 01:33
  • Now I get this exception in the console: 2015-03-21 18:42:50.518 MyList[29808:1235523] CoreData: warning: Unable to load class named 'MyList.List' for entity 'List'. Class not found, using default NSManagedObject instead. – J0NNY ZER0 Mar 22 '15 at 01:43
  • So I don't know why but most tutorials instruct me to apply the app name plus a dot before the class in the class field in the entity editor but when I removed this and just put in the class name in the class field of the entity editor it worked fine. – J0NNY ZER0 Mar 22 '15 at 01:56
  • @J0NNYZER0 Your Core Data question is a good one but it's different than the one you asked here. If you search Stack Overflow and don't find the answer, feel free to post a new question. – Aaron Brager Mar 22 '15 at 02:46
0

I think you have to add

@objc(List)

over this line:

class List: NSManagedObject ...

This was the solution posted in this link.

Community
  • 1
  • 1
Dario
  • 3,105
  • 1
  • 17
  • 16