无法将类型 'Int' 的值转换为预期的参数类型 '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void'
Cannot convert value of type 'Int' to expected argument type '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void'
这一行 var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: 0)
抛出错误。
错误信息:
Cannot convert value of type 'Int' to expected argument type '(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"
}
}
inout
参数是那些将被我们传递给它的函数修改的参数。因此,您需要将其作为 &size
.
传递
我得到了答案,
initializingWith: {_,_ in}
像那样:
var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: {_,_ in})
这一行 var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: 0)
抛出错误。
错误信息:
Cannot convert value of type 'Int' to expected argument type '(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"
}
}
inout
参数是那些将被我们传递给它的函数修改的参数。因此,您需要将其作为 &size
.
我得到了答案,
initializingWith: {_,_ in}
像那样:
var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: {_,_ in})