Help us understand the problem. What is going on with this article?

Swift の Array やら ArraySlice やらポインタの変換まとめ

More than 1 year has passed since last update.

Swift の以下の型の間で、直接相互に変換できるものを網羅してみる。

UnsafeRawPointer
UnsafeMutableRawPointer

UnsafePointer< Int >
UnsafeMutablePointer< Int >

UnsafeRawBufferPointer
UnsafeMutableRawBufferPointer

UnsafeBufferPointer< Int >
UnsafeMutableBufferPointer< Int >

Array< Int >
ArraySlice< Int >

OpaquePointer
ポインタ名 変更 サイズ
UnsafeRawPointer
UnsafeMutableRawPointer
UnsafePointer< Int >
UnsafeMutablePointer< Int >
UnsafeRawBufferPointer
UnsafeMutableRawBufferPointer
UnsafeBufferPointer< Int >
UnsafeMutableBufferPointer< Int >
let Array< Int >
var Array< Int >
let ArraySlice< Int >
var ArraySlice< Int >
OpaquePointer
  • 変更できないものからできるものには変換するには、mutating:を使う。
  • 型のないものからあるものには変換するには、assumingMemoryBound( to: Int.self )を使う。
  • サイズのないものからあるものには変換できない。
  • withUnsafeRawBufferPointer という名前のメソッドはない。withUnsafeBytesを使う。
  • withUnsafeMutableRawBufferPointer という名前のメソッドはない。withUnsafeMutableBytesを使う。

var Array< Int > から

var w   : Array< Int >                      = [ 1, 2, 3 ]

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w )
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: w )

let _   : UnsafePointer< Int >              = UnsafePointer( w )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: w )

let _   : UnsafeRawBufferPointer            = w.withUnsafeBytes { $0 }
let _   : UnsafeMutableRawBufferPointer     = w.withUnsafeMutableBytes { $0 }

let _   : UnsafeBufferPointer< Int >        = w.withUnsafeBufferPointer { $0 }
let _   : UnsafeMutableBufferPointer< Int > = w.withUnsafeMutableBufferPointer { $0 }

let _   : ArraySlice< Int >                 = ArraySlice( w )

let _   : OpaquePointer                     = OpaquePointer( w )

let Array< Int > から

let w   : Array< Int >                      = [ 1, 2, 3 ]

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w )
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: w )

let _   : UnsafePointer< Int >              = UnsafePointer( w )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: w )

let _   : UnsafeRawBufferPointer            = w.withUnsafeBytes { $0 }
let _   : UnsafeMutableRawBufferPointer     = UnsafeMutableRawBufferPointer( mutating: w.withUnsafeBytes { $0 } )

let _   : UnsafeBufferPointer< Int >        = w.withUnsafeBufferPointer { $0 }
let _   : UnsafeMutableBufferPointer< Int > = UnsafeMutableBufferPointer( mutating: w.withUnsafeBufferPointer { $0 } )

let _   : ArraySlice< Int >                 = ArraySlice( w )

let _   : OpaquePointer                     = OpaquePointer( w )

var ArraySlice< Int > から

var w   : ArraySlice< Int >                 = ArraySlice( [ 1, 2, 3 ] )

let _   : UnsafeRawPointer                  = w.withUnsafeBytes { $0.baseAddress! }
let _   : UnsafeMutableRawPointer           = w.withUnsafeMutableBytes { $0.baseAddress! }

let _   : UnsafePointer< Int >              = w.withUnsafeBufferPointer { $0.baseAddress! }
let _   : UnsafeMutablePointer< Int >       = w.withUnsafeMutableBufferPointer { $0.baseAddress! }

let _   : UnsafeRawBufferPointer            = w.withUnsafeBytes { $0 }
let _   : UnsafeMutableRawBufferPointer     = w.withUnsafeMutableBytes { $0 }

let _   : UnsafeBufferPointer< Int >        = w.withUnsafeBufferPointer { $0 }
let _   : UnsafeMutableBufferPointer< Int > = w.withUnsafeMutableBufferPointer { $0 }

let _   : Array< Int >                      = Array( w )

let _   : OpaquePointer                     = OpaquePointer( w.withUnsafeBufferPointer { $0.baseAddress! } )

let ArraySlice< Int > から

let w   : ArraySlice< Int >                 = ArraySlice( [ 1, 2, 3 ] )

let _   : UnsafeRawPointer                  = w.withUnsafeBytes { $0.baseAddress! }
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: w.withUnsafeBytes { $0.baseAddress! } )

let _   : UnsafePointer< Int >              = w.withUnsafeBufferPointer { $0.baseAddress! }
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: w.withUnsafeBufferPointer { $0.baseAddress! } )

let _   : UnsafeRawBufferPointer            = w.withUnsafeBytes { $0 }
let _   : UnsafeMutableRawBufferPointer     = UnsafeMutableRawBufferPointer( mutating: w.withUnsafeBytes { $0 } )

let _   : UnsafeBufferPointer< Int >        = w.withUnsafeBufferPointer { $0 }
let _   : UnsafeMutableBufferPointer< Int > = UnsafeMutableBufferPointer( mutating: w.withUnsafeBufferPointer { $0 } )

let _   : Array< Int >                      = Array( w )

let _   : OpaquePointer                     = OpaquePointer( w.withUnsafeBufferPointer { $0.baseAddress! } )

UnsafeRawPointer から

let w   : UnsafeRawPointer                  = UnsafeRawPointer( [ 1, 2, 3 ] )

let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: w )

let _   : UnsafePointer< Int >              = w.assumingMemoryBound( to: Int.self )
Int.self )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: w.assumingMemoryBound( to: Int.self ) )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutableRawPointer( mutating: w ).assumingMemoryBound( to:  Int.self ) )   // こんなのも可能

let _   : OpaquePointer                     = OpaquePointer( w )

UnsafeMutableRawPointer から

let w   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: [ 1, 2, 3 ] )

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w )

let _   : UnsafePointer< Int >              = UnsafePointer( w.assumingMemoryBound( to: Int.self ) )
let _   : UnsafeMutablePointer< Int >       = w.assumingMemoryBound( to: Int.self )

let _   : OpaquePointer                     = OpaquePointer( w )

UnsafePointer< Int > から

let w   : UnsafePointer< Int >              = UnsafePointer( [ 1, 2, 3 ] )

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w )
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: w )

let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: w )

let _   : OpaquePointer                     = OpaquePointer( w )

UnsafeMutablePointer< Int > から

let w   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: [ 1, 2, 3 ] )

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w )
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( w )

let _   : UnsafePointer< Int >              = UnsafePointer( w )

let _   : OpaquePointer                     = OpaquePointer( w )

UnsafeRawBufferPointer から

let w   : UnsafeRawBufferPointer            = [ 1, 2, 3 ].withUnsafeBytes { $0 }

let _   : UnsafeRawPointer                  = w.baseAddress!
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: w.baseAddress! )

let _   : UnsafePointer< Int >              = w.baseAddress!.assumingMemoryBound( to: Int.self )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: w.baseAddress!.assumingMemoryBound( to: Int.self ) )

let _   : UnsafeMutableRawBufferPointer     = UnsafeMutableRawBufferPointer( mutating: w )

let _   : OpaquePointer                     = OpaquePointer( w.baseAddress! )

UnsafeMutableRawBufferPointer から

var wArray = [ 1, 2, 3 ]
let w   : UnsafeMutableRawBufferPointer     = wArray.withUnsafeMutableBytes { $0 }

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w.baseAddress! )
let _   : UnsafeMutableRawPointer           = w.baseAddress!

let _   : UnsafePointer< Int >              = UnsafePointer( w.baseAddress!.assumingMemoryBound( to: Int.self ) )
let _   : UnsafeMutablePointer< Int >       = w.baseAddress!.assumingMemoryBound( to: Int.self )

let _   : UnsafeRawBufferPointer            = UnsafeRawBufferPointer( w )

let _   : OpaquePointer                     = OpaquePointer( w.baseAddress! )

UnsafeBufferPointer< Int > から

let w   : UnsafeBufferPointer               = [ 1, 2, 3 ].withUnsafeBufferPointer { $0 }

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w.baseAddress! )
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( mutating: w.baseAddress! )

let _   : UnsafePointer< Int >              = UnsafePointer( w.baseAddress! )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( mutating: w.baseAddress! )

let _   : UnsafeRawBufferPointer            = UnsafeRawBufferPointer( w )
let _   : UnsafeMutableRawBufferPointer     = UnsafeMutableRawBufferPointer( mutating: UnsafeRawBufferPointer( w ) )

let _   : UnsafeMutableBufferPointer< Int > = UnsafeMutableBufferPointer( mutating: w )

let _   : Array< Int >                      = Array( w )
let _   : ArraySlice< Int >                 = ArraySlice( w )

let _   : OpaquePointer                     = OpaquePointer( w.baseAddress! )

UnsafeMutableBufferPointer< Int > から

var wArray = [ 1, 2, 3 ]
let w   : UnsafeMutableBufferPointer        = wArray.withUnsafeMutableBufferPointer { $0 }

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w.baseAddress! )
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( w.baseAddress! )

let _   : UnsafePointer< Int >              = UnsafePointer( w.baseAddress! )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( w.baseAddress! )

let _   : UnsafeRawBufferPointer            = UnsafeRawBufferPointer( w )
let _   : UnsafeMutableRawBufferPointer     = UnsafeMutableRawBufferPointer( w )

let _   : UnsafeBufferPointer< Int >        = UnsafeBufferPointer( w )

let _   : Array< Int >                      = Array( w )
let _   : ArraySlice< Int >                 = ArraySlice( w )

let _   : OpaquePointer                     = OpaquePointer( w.baseAddress! )

OpaquePointer から

let w   : OpaquePointer                     = OpaquePointer( [ 1, 2, 3 ] )

let _   : UnsafeRawPointer                  = UnsafeRawPointer( w )
let _   : UnsafeMutableRawPointer           = UnsafeMutableRawPointer( w )

let _   : UnsafePointer< Int >              = UnsafePointer( w )
let _   : UnsafeMutablePointer< Int >       = UnsafeMutablePointer( w )
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away