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

Swift incorrectly fills in ivar attribute for static properties in categories

    XMLWordPrintable

    Details

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

      Tested on Swift 4.2, will try on a nightly shortly.

      Description

      Objective-C does not support ivars on classes, and does not support adding ivars in categories. You can then imagine my surprise when I found programs that claim that they have class properties, defined in categories, that are backed by ivars.

      The Swift compiler appears to co-opt the ivar field to put the name of the variable that holds the value of a static property, even though that variable is not an ivar.

      Build this:

      import Foundation
      
      @objc public extension Formatter {
          @objc static var iso8601: Int = 0
      }
      

      then inspect it with your favorite disassembler. You can see that the NSFormatter (test) category defines an "iso8601" class property with the type string "Tq,N,Viso8601" (data type signed long long, nonatomic, backed by ivar called iso8601).

        Attachments

          Activity

            People

            Assignee:
            fclout Félix Cloutier
            Reporter:
            fclout Félix Cloutier
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: