14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-11-26

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 )
14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?