Uploaded image for project: 'Swift'
  1. Swift
  2. SR-10337

Swift 5 regression: Incorrect type cast from AnyObject to incompatible NSObject subclass.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Component/s: None
    • Environment:

      Xcode 10.2 (10E125)

      Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
      Target: x86_64-apple-darwin18.5.0

      Description

       

      The following code crashes in Xcode 10.2 as the model is incorrectly cast to an NSManagedObject instance:

      Unrecognized selector -[__lldb_expr_52.MyModel objectID]

      This is not a problem in Xcode 10.1.

      import CoreData
      
      public protocol MyProtocol: class {}
      public class MyModel: MyProtocol {}
      
      let model: AnyObject = MyModel()
      
      func perform() {
          guard let managedModel = model as? (NSManagedObject & MyProtocol) else { return }
          print(managedModel.objectID)
      }
      
      perform()
      

       

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            Leitch Ian Leitch
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: