如何在 Swift 5.1 中修复 "Cannot invoke 'send' with no arguments"

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

我一直在努力关注 'Introducing SwiftUI - Building Your First App' WWDC 19 视频。本次演讲没有提供示例代码,但我一直在随着演示者的进行而创建它。虽然我在尝试创建商店时收到错误消息“无法调用 'send' with no arguments”,但行如下:

didSet { didChange.send() }

我是编程新手,正在努力解决问题。

import SwiftUI
import Combine

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

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

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

如果有帮助,我正在使用 Xcode 11 beta 和 MacOS Catalina。

将您的代码替换为

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

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

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

PassthroughSubject<Void, Never> 是您的发布商,声明为:

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

这是send函数:

final func send(_ input: Output)

这意味着 send 需要一个 Void 参数,在 Swift 中是空元组 ().

替换:

didChange.send()

didChange.send(())

Xcode 11 beta 4中,您可以只使用send()实例方法。

func send()

用法:

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

更新 :

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.

所以将代码更改为

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

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

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