简体   繁体   English

Swift:调用中缺少参数的参数

[英]Swift: Missing argument for parameter in call

Hi I am new to Swift, but I've been working on a project to compile a Flutter app into iOS.嗨,我是 Swift 的新手,但我一直在致力于将 Flutter 应用程序编译到 iOS 的项目。 Below is the portion of the code in Xcode (version 13) where there are errors.下面是 Xcode(版本 13)中存在错误的部分代码。

struct QualifiedCharacteristic: Equatable {
    let id: CharacteristicID
    let serviceID: ServiceID
    let peripheralID: PeripheralID
}


extension QualifiedCharacteristic {
    init(_ characteristic: CBCharacteristic) {
        self.init(
            id: characteristic.uuid,
            serviceID: characteristic.service?.uuid ??,
            peripheralID: characteristic.service?.peripheral
        )
    }
}
  1. While initialising the three attributes, the first error says "Missing arguments or parameter 'serviceID' in call" and "Missing arguments or parameter 'serviceID' in call, Insert ',serviceID: <#ServiceID#>".在初始化这三个属性时,第一个错误是“调用中缺少参数或参数‘serviceID’”和“调用中缺少参数或参数‘serviceID’,插入‘,serviceID:<#ServiceID#>”。 When I click on the "Fix" button behind the second error message, it just add another ",serviceID: ServiceID" behind the id attribute.当我单击第二条错误消息后面的“修复”按钮时,它只是在 id 属性后面添加了另一个“,serviceID:ServiceID”。 I was wondering what I did wrong there because there is already a serviceID attribute initialised right after id attribute.我想知道我在那里做错了什么,因为在 id 属性之后已经有一个 serviceID 属性初始化。

  2. The second error is after the comma behind serviceID initialisation.第二个错误是在 serviceID 初始化后面的逗号之后。 The error message says that "Expected expression after operator".错误消息说“运算符之后的预期表达式”。

I appreciate everyone's time who can look into these errors.我感谢每个人都可以调查这些错误的时间。

If the characteristic doesn't have a service, then your init can fail:如果特征没有服务,那么您的 init 可能会失败:

change改变

init(_ characteristic: CBCharacteristic) {

to be a failable initializer成为一个可失败的初始化程序

init?(_ characteristic: CBCharacteristic) {

then guard let the values you need to pass to init然后保护让你需要传递给init的值

guard let service = characteristic.service else { return nil }

now you have an honest, non-optional service, so you have the values you need to pass those to the other init:现在你有了一个诚实的、非可选的服务,所以你有了将这些值传递给另一个 init 所需的值:

    self.init(
        id: characteristic.uuid,
        serviceID: service.serviceID,
        peripheralID: service.peripheral
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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