Ios swift生成错误我将字节数组更改为除前几个字节以外的所有字节

Ios swift生成错误我将字节数组更改为除前几个字节以外的所有字节,ios,swift,xcode,Ios,Swift,Xcode,我想删除数组的前n个字节,但如果使用dropFirst或removeFirst,则生成错误。 最后,我希望累积接收字节的所有字节都位于累积接收字节中第一个字节之后 var cumulative_receive_bytes = Array<UInt8>(repeating: 0, count: 1024) . . . var latest_hub_bytes = Array<UInt8>(repeating: 0, count: 1024) . . . cumu

我想删除数组的前n个字节,但如果使用dropFirst或removeFirst,则生成错误。
最后,我希望累积接收字节的所有字节都位于累积接收字节中第一个字节之后

var cumulative_receive_bytes = Array<UInt8>(repeating: 0, count: 1024)  
. . .  
var latest_hub_bytes = Array<UInt8>(repeating: 0, count: 1024)  
. . .
cumulative_receive_bytes += latest_hub_bytes
let hub_string = String(bytes: cumulative_receive_bytes, encoding: .ascii)
let hub_lines = hub_string!.split( whereSeparator: \.isNewline)
let message_from_hub = hub_lines[ 0 ]

cumulative_receive_bytes = cumulative_receive_bytes.removeFirst( message_from_hub.count + 1 )  
... ERROR IS:  Cannot assign value of type '()' to type '[UInt8]'   <<<<<<<<<<<<<<<<<<

cumulative_receive_bytes = cumulative_receive_bytes.dropFirst( message_from_hub.count + 1 )  
... ERROR IS:  Cannot assign value of type 'Array<UInt8>.SubSequence' (aka 'ArraySlice<UInt8>') to type '[UInt8]'  <<<<<<<<<<<
var累计接收字节=数组(重复:0,计数:1024)
. . .  
var latest_hub_bytes=数组(重复:0,计数:1024)
. . .
累计接收字节+=最新的集线器字节
let hub_string=string(字节:累计接收字节,编码:.ascii)
让hub\u line=hub\u string!。拆分(其中分隔符:\.isNewline)
让消息\u从\u中心=中心线[0]
累计接收字节数=累计接收字节数.removeFirst(消息\u来自\u hub.count+1)

... 错误是:无法将类型“()”的值赋给类型“[UInt8]”
removeFirst
是一个变异函数-它直接变异数组。它不会返回修改后的数组。这就是为什么会出现无法为数组分配函数(键入
()
)的错误

你可以说

cumulative_receive_bytes.removeFirst( message_from_hub.count + 1 )
在第二种情况下,
dropFirst
返回一个
ArraySlice
——您需要使用它来创建一个新的数组

cumulative_receive_bytes = Array(cumulative_receive_bytes.dropFirst( message_from_hub.count + 1 ))

关于样式的注意事项,在Swift中,您应该将camelCase用于变量,而不是“累计接收字节”
not
cumulativeReceiveBytes

Mmmm--同意这是正确的样式,并且最容易键入。然而,几十年前,我做了一个职业决定,在代码和注释中尽可能地遵循英语语法和语法,因为英语的可读性元素是一个经过调试的“系统”——大多数情况下——而且我发现在大量代码中,下划线比所有单词挤在一起更容易阅读。但我会考虑骆驼。谢谢。如果只是你的代码,那可能没关系,但是如果你和其他Swift程序员一起工作,那么最好还是坚持你工作语言的惯用风格。