Cannot pass immutable value as inout argument: function call returns immutable value

空扰寡人 提交于 2019-12-04 06:29:22

问题


I forked this project, so I am not as familiar with all of the details: https://github.com/nebs/hello-bluetooth/blob/master/HelloBluetooth/NSData%2BInt8.swift.

This is all part of an extension of NSData that the I am using to send 8-bit values to an Arduino.

func int8Value() -> Int8 {
    var value: Int8 = 0
    copyBytes(to: &UInt8(value), count: MemoryLayout<Int8>.size)    //BUG

    return value
}

However, it appears in Swift 3 that this now throws an error in the copyBytes section. Although I have seen some solutions such as passing an address in the parameter, I did not want to risk breaking the remaining parts of the code. Any suggestions on what to do for this?


回答1:


The original code was incorrect. UInt8(value) generates a new, immutable value which you cannot write to. I assume the old compiler just let you get away with it, but it was never correct.

What they meant to do was to write to the expected type, and then convert the type at the end.

extension Data {
    func int8Value() -> Int8 {
        var value: UInt8 = 0
        copyBytes(to: &value, count: MemoryLayout<UInt8>.size)

        return Int8(value)
    }
}

That said, I wouldn't do it that way today. Data will coerce its values to whatever type you want automatically, so this way is safer and simpler and very general:

extension Data {
    func int8ValueOfFirstByte() -> Int8 {
        return withUnsafeBytes{ return $0.pointee }
    }
}

Or this way, which is specific to ints (and even simpler):

extension Data {
    func int8Value() -> Int8 {
        return Int8(bitPattern: self[0])
    }
}


来源:https://stackoverflow.com/questions/41653043/cannot-pass-immutable-value-as-inout-argument-function-call-returns-immutable-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!