简体   繁体   中英

ambiguous use of method

My build completes successfully. Then after some time this error pops up:

"Ambiguous use of 'addObjectsDidChangeNotificationObserver(handler:)'"

What I do not understand is why this happens because the addObjectsDidChangeNotificationObserver method is only declared once in the project and the second occurence shown by Xcode is the use of the method itself.

找到候选人1

错误位置

Here is the code where the error is shown and which Xcode also shows me as first candidate:

public init?(object: Managed, changeHandler: @escaping (ChangeType) -> ()) {
    guard let moc = object.managedObjectContext else { return nil }

    objectHasBeenDeleted = !type(of: object).defaultPredicate.evaluate(with: object)

    token = moc.addObjectsDidChangeNotificationObserver(handler: {
        [unowned self] note in
        guard let changeType = self.changeType(of: object, in: note) else { return }
        self.objectHasBeenDeleted = changeType == .delete
        changeHandler(changeType)
    })
}

and the implementation of addObjectsDidChangeNotificationObserver() , which Xcode shows me as second candidate:

extension NSManagedObjectContext {
    public func addObjectsDidChangeNotificationObserver(handler: @escaping (ObjectsDidChangeNotification) -> ()) -> NSObjectProtocol {
        let nc = NotificationCenter.default
        return nc.addObserver(forName: .NSManagedObjectContextObjectsDidChange, object: self, queue: nil) { note in
            let wrappedNote = ObjectsDidChangeNotification(note: note)
            handler(wrappedNote)
        }
    }
}

Ok, the problem seems to be solved now.

Apparently, I messed up with the access modifiers, but good to know that something like that can cause an ambiguous error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM