简体   繁体   English

将默认值或 nil 值设置为函数的泛型类型参数

[英]Set default or nil value to the generic type parameter of the function

Let's say I have one protocol假设我有一个协议

protocol TestProtocol {
}

Also, have one struct and inherited it from protocol.此外,有一个结构并从协议继承它。

struct StructOne: TestProtocol {
}

Now, I have one view controller class and created one generic function to accept an array of TestProtocol type objects(This is a generic arugment).现在,我有一个视图控制器类并创建了一个通用函数来接受一组TestProtocol类型对象(这是一个通用参数)。 This is for the passing parameter for SDK API calls.这是用于 SDK API 调用的传递参数。

But in some API calls, I do not need to pass this parameter array.但是在一些 API 调用中,我不需要传递这个参数数组。 So, I just wanted to set nil value or default empty array within the function definition.所以,我只想在函数定义中设置 nil 值或默认空数组。

Here is the class这是课堂

class TestViewController: UIViewController {
//  func genericCall<T: TestProtocol>(param: [T] = []) { // Not work
//  func genericCall<T: TestProtocol>(param: [T]? = nil) { // Not work
  func genericCall<T: TestProtocol>(param: [T]?) {
    if param?.isEmpty == true {
      print("Empty Param Calling")
    } else {
      print("With Param Calling")
    }
  }
   
  override func viewDidLoad() {
    super.viewDidLoad()
    let param = [StructOne(), StructOne()]
    self.genericCall(param: param) // This one work
    self.genericCall(param: [] as [StructOne]) // This one also work. But want default value in function
    self.genericCall(param: nil) // Not work : Error - Generic parameter 'T' could not be inferred
//    self.genericCall() // Not work with default empty value : Error - Generic parameter 'T' could not be inferred
  }
}

I am getting this compile-time error: Generic parameter 'T' could not be inferred我收到此编译时错误:无法推断通用参数“T”

I can set an empty array during the function call.我可以在函数调用期间设置一个空数组。 which is mention here这是这里提到的

I also check this link , which allows setting nil value if have only T type, but in here is the array of T ([T]).我还检查了这个链接,如果只有 T 类型,它允许设置 nil 值,但这里是 T ([T]) 的数组。

Is there any way to set the default nil value or any other way to set default an empty array?有没有办法设置默认的 nil 值或任何其他方法来设置默认的空数组? so we can avoid passing an empty array to each function call.所以我们可以避免向每个函数调用传递一个空数组。

Update:更新:

I can't use it this way.我不能这样用。 As SDK function call not allowed me to pass param value.由于 SDK 函数调用不允许我传递参数值。

func genericCall(param: [TestProtocol] = []) {
    // param: Not allowed me to pass to the sdk call function.
    if param.isEmpty == true {
        print("Empty Param Calling")
    } else {
        print("With Param Calling")
    }
}

Note: This is a demo code.注意:这是一个演示代码。 In reality, I'm using one of the SDK so, I cant change more in the protocol.实际上,我正在使用 SDK 之一,因此我无法在协议中进行更多更改。

nil is too broad in this case, as the compiler can't infer to which [T]?在这种情况下nil太宽泛了,因为编译器无法推断出哪个[T]? it should apply the nil.它应该适用零。

You will need to explicitly specify the Optional generic argument here:您需要在此处明确指定Optional通用参数:

self.genericCall(param: [StructOne]?.none)

You can create a generic method constraining your generic type to RangeReplaceableCollection and its Element to your TestProtocol .您可以创建一个泛型方法,将您的泛型类型限制为RangeReplaceableCollection并将其Element为您的TestProtocol The reason it will work is that RangeReplaceableCollection requires the protocols that conform to it to provide an empty initializer:它能工作的原因是RangeReplaceableCollection要求符合它的协议提供一个空的初始化程序:


protocol TestProtocol { }

struct StructOne: TestProtocol { }

class TestViewController: UIViewController {

    func genericCall<T: RangeReplaceableCollection>(param: T = .init()) -> T where T.Element:  TestProtocol {
        if param.isEmpty {
          print("Empty Param Calling")
        } else {
          print("With Param Calling")
        }
        return param
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let result: [StructOne] = genericCall() // This one work
        print(result)
    }
}

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

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