简体   繁体   中英

Cannot convert value of type 'Int' to expected argument type '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void'

This line var machine = [CChar](_unsafeUninitializedCapacity: size, initializingWith: 0) error throwing.

Error Message:

Cannot convert value of type 'Int' to expected argument type '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void'

This is my Code:

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 parameters are those that are going to be modified by the function we are passing it to. Therefore, you need to pass it as &size .

I got Answer,

initializingWith: {_,_ in}

Like that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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