【问题标题】:Cannot convert value of type 'Int' to expected argument type '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void'无法将“Int”类型的值转换为预期的参数类型“(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void”
【发布时间】:2019-05-15 05:07:24
【问题描述】:

这行var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: 0) 抛出错误。

错误信息:

无法将“Int”类型的值转换为预期的参数类型“(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void”

这是我的代码:

struct MailTemplate {
    let destination = "test@gmail.com"
    let subject = "Test"
    let body:String

    init(){
        let appVersion: String! = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
        var platform: String {
            var size: Int = 0
            sysctlbyname("hw.machine", nil, &size, nil, 0)
            var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: 0)
            sysctlbyname("hw.machine", &machine, &size, nil, 0)
            return String.fromCString(machine)!
        }
        body = "Email Body"
    }
} 

【问题讨论】:

    标签: ios swift4


    【解决方案1】:

    我得到了答案,

    initializingWith: {_,_ in}
    

    这样:

    var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: {_,_ in})
    

    【讨论】:

      【解决方案2】:

      inout 参数是那些将被我们传递给它的函数修改的参数。因此,您需要将其传递为&amp;size

      【讨论】:

      • 当我传递大小时,我得到了这个错误:无法将类型“Int”的值转换为预期的参数类型“(inout UnsafeMutableBufferPointer,inout Int)抛出-> Void'(又名'( inout UnsafeMutableBufferPointer, inout Int) throws -> ()')
      • 你需要传递一个函数。 '(x, y) throws -> Void' 是一个函数签名,您将 int 传递给它。但是inout参数需要按照我上面提到的方式发送
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      相关资源
      最近更新 更多