Skip to content

[SR-6419] NotificationCenter.addObserver of SCLF with trailing closure syntax cannot be used on Swift 4.0.2 #3786

Closed

Description

@ikesyo
mannequin
Previous ID SR-6419
Radar None
Original Reporter @ikesyo
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @ikesyo
Priority Medium

md5: 1cb775e2d2830c261d8a0e8e57a0fbcd

Issue Description:

In Swift 4.0.2 NotificationCenter.addObserver(forName:object:queue:usingBlock🙂 is renamed to addObserver(forName:object:queue:using🙂 and the old API is kept as an obsoleted API. The obsoleted symbol makes it ambiguous to use the API with trailing closure syntax:

self.token = self.notificationCenter.addObserver(forName: nil, object: nil, queue: nil) { [weak self] n in
    // linux-swift gets confused by .append(n)
    self?.observedNotifications.append(n)
}

This is found at Quick/Nimble#481

/home/travis/build/lvsti/MockSix/.build/checkouts/Nimble.git--3756397004363768916/Sources/Nimble/Matchers/PostNotification.swift:18:27: error: ambiguous use of 'addObserver'
        self.token = self.notificationCenter.addObserver(forName: nil, object: nil, queue: nil) { [weak self] n in
                          ^

Foundation.NotificationCenter:9:15: note: found this candidate
    open func addObserver(forName name: Foundation.NSNotification.Name?, object obj: Any?, queue: Foundation.OperationQueue?, usingBlock block: @escaping (Foundation.Notification) -> Swift.Void) -> NSObjectProtocol
              ^

Foundation.NotificationCenter:10:15: note: found this candidate
    open func addObserver(forName name: Foundation.NSNotification.Name?, object obj: Any?, queue: Foundation.OperationQueue?, using block: @escaping (Foundation.Notification) -> Swift.Void) -> NSObjectProtocol
              ^

Usually we don't keep old (pre-renamed) symbols (e.g. #1315 so the obsoleted API should be removed to make it unambiguous.

Activity

  1. belkadan commented on Nov 17, 2017

    @belkadan

    @phausler There was a dup of this for macOS, right?

  2. ikesyo commented on Nov 23, 2017

    ikesyomannequin
    MannequinAuthor

    @available(*, obsoleted: 4.0) is not appropriate and @available(swift, obsoleted: 4.0) should be used instead.

  3. ikesyo commented on Nov 23, 2017

    ikesyomannequin
    MannequinAuthor

    I was requested to use @available(*, unavailable) instead of @available(swift, obsoleted: 4.0).

  4. ikesyo commented on Nov 23, 2017

    ikesyomannequin
    MannequinAuthor

    So the actual issue here is @available(*, obsoleted: 4.0) means iOS 4.0, macOS 4.0, tvOS 4.0, watchOS 4.0 and does not mean Swift 4.0.

  5. belkadan commented on Nov 27, 2017

    @belkadan

    That's hilarious. We should definitely be rejecting that!

  6. alblue commented on Nov 28, 2017

    @alblue
    Contributor

    This has been merged into the master branch, which should be part of the 4.1 release.

  7. transferred this issue fromswiftlang/swifton May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions