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

"\(Self.self)" causes issue on Xcode 12.5 when Swift is optimized

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler, SwiftSyntax
    • Labels:
      None

      Description

      Using Xcode 12.5 I have identified an issue with this instruction "(Self.self)" when it runs on a build created setting SWIFT_OPTIMIZATION_LEVEL to -O (Optimize for speed).

      The instruction above is executed in a superclass (subclass of UIView) when it to load a NIB:

      Bundle.main.loadNibNamed("\(Self.self)", owner: self, options: nil)
      

      Instead of getting the right subclass name, it simply returns "Self" and this generate the following error:

      *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: '...' with name 'Self''
      

      This never happens before with previous versions of Xcode, and it still does not happen when the build is generate without using optimization -Onone.

      To fix it I have changed "(Self.self)" with "(type(of: self))". This way it works, but I think that it should be investigate why the original version causes that kind of issue when Swift is optimized.

      Forcing optimization in Debug, I was able to reproduce the issue. I have also tried to insert a breakpoint before the crash, trying to execute the instruction in Console with po command, but it works as expected, printing the right class name.

       

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            matteolongeri_ovolab Matteo Longeri
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: