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

target self should be illegal in property constructor

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Duplicate
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Xcode 8.3.2

      Description

      This is legal in an instance property declaration:

          let listen : UIBarButtonItem = {
              let b = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(doListen))
              return b
          }()
      

      It shouldn't be. There is no `self` at this point. The code compiles but it isn't going to work, so it shouldn't compile at all.

      I see a lot of people on Stack Overflow get caught by this sort of thing, but this is the first time I've made the same mistake myself. The usual context is in configuring a Timer.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              mattneub Matt Neuburg
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: