How to fix "Cannot invoke 'send' with no arguments" in Swift 5.1

Viewed 1352

I've been trying to follow the 'Introducing SwiftUI - Building Your First App' WWDC 19 video. No sample code is provided for this talk but I've been creating it as the presenter goes along. When trying to create a store though I get an error that 'Cannot invoke 'send' with no arguments' from the line:

didSet { didChange.send() }

I'm new to programming and struggling to troubleshoot.

import SwiftUI
import Combine

class ReferenceStore : BindableObject {
    var references: [Reference] {
        didSet { didChange.send() }
    }

    init(references: [Reference] = []) {
        self.references = references
    }

    var didChange = PassthroughSubject<Void, Never>()
}

I'm using Xcode 11 beta and MacOS Catalina if it helps.

3 Answers

PassthroughSubject<Void, Never> is your publisher, and it's declared as:

final class PassthroughSubject<Output, Failure> where Failure : Error

And this is send function:

final func send(_ input: Output)

That means send needs a Void argument, which in Swift is the empty tuple ().

Replace:

didChange.send()

with

didChange.send(())

Replace your code to

class ReferenceStore : BindableObject {
    var references: [Reference] {
        didSet { didChange.send(self) }
    }

    init(references: [Reference] = []) {
        self.references = references
    }

    var didChange = PassthroughSubject<ReferenceStore, Never>()
}

In Xcode 11 beta 4, you can just use send() instance method.

func send()

Usage :

var references: [Reference] {
    didSet { didChange.send() }
}

Update :

The BindableObject protocol’s requirement is now willChange instead of didChange, and should now be sent before the object changes rather than after it changes. This change allows for improved coalescing of change notifications.

So change your code to

class ReferenceStore : BindableObject {
    var references: [Reference] {
        didSet { willChange.send() }
    }

    init(references: [Reference] = []) {
        self.references = references
    }

    var willChange = PassthroughSubject<Void, Never>()
}
Related