Generic Instance Method 総称体インスタンスメソッド

withUnsafeMutableBytes(_:)

Calls the given closure with a pointer to the underlying bytes of the array’s mutable contiguous storage. 与えられたクロージャを、配列の可変の隣接ストレージの基礎をなすバイトへのポインタとともに呼び出します。

Declaration 宣言

mutating func withUnsafeMutableBytes<R>(_ body: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R

Parameters パラメータ

body

A closure with an UnsafeMutableRawBufferPointer parameter that points to the contiguous storage for the array. If no such storage exists, it is created. If body has a return value, that value is also used as the return value for the withUnsafeMutableBytes(_:) method. The argument is valid only for the duration of the closure’s execution. あるクロージャでUnsafeMutableRawBufferPointerパラメータを持ち、それはその配列のための隣接ストレージを指し示します。そのようなストレージが存在しないならば、それは作成されます。bodyが戻り値を持つならば、その値はまたwithUnsafeMutableBytes(_:)メソッドの戻り値としても使われます。引数は、ただクロージャの実行の間に対してのみ有効とされます。

Return Value 戻り値

The return value, if any, of the body closure parameter. bodyクロージャパラメータの戻り値、もしあれば。

Discussion 解説

The array’s Element type must be a trivial type, which can be copied with just a bit-for-bit copy without any indirection or reference-counting operations. Generally, native Swift types that do not contain strong or weak references are trivial, as are imported C structs and enums. この配列の持つElement型は自明型でなければなりません、それは単にビット対ビットコピーでコピーされることが、何らかの間接参照または参照カウント操作なしで可能です。一般に、生粋のSwift型で強いまたは弱い参照を含まないものは自明です、インポートされたCのstructとenumのように。

The following example copies bytes from the byteValues array into numbers, an array of Int32: 以下の例は、バイトをbyteValues配列から、numbersの配列Int32へとコピーします:


var numbers: [Int32] = [0, 0]
var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]


numbers.withUnsafeMutableBytes { destBytes in
    byteValues.withUnsafeBytes { srcBytes in
        destBytes.copyBytes(from: srcBytes)
    }
}
// numbers == [1, 2]

The pointer passed as an argument to body is valid only for the lifetime of the closure. Do not escape it from the closure for later use. 引数としてbodyに渡されるポインタは、このクロージャの生涯に対してのみ有効です。それを後で使うためにクロージャから脱出させないでください。

See Also 参照

Accessing Underlying Storage 基礎をなすストレージにアクセスする