2017-04-14 34 views
1

IはSWIFであるthis link からのコードを使用する2array.withUnsafeMutableBufferPointerをswift 2からswift 3に移行するにはどうすればよいですか?

public protocol SGLImageType { 
    typealias Element 
    var width:Int {get} 
    var height:Int {get} 
    var channels:Int {get} 
    var rowsize:Int {get} 

    func withUnsafeMutableBufferPointer(
     @noescape body: (UnsafeMutableBufferPointer<Element>) throws -> Void 
    ) rethrows 
} 

プロトコル上に実装クラス:

'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type

:3迅速に

final public class SGLImageRGBA8 : SGLImageType { ... public func withUnsafeMutableBufferPointer(@noescape body: (UnsafeMutableBufferPointer<UInt8>) throws -> Void) rethrows { 
    try array.withUnsafeMutableBufferPointer(){ 
     // This is unsafe reinterpret cast. Be careful here. 
     let st = UnsafeMutablePointer<UInt8>($0.baseAddress) 
     try body(UnsafeMutableBufferPointer<UInt8>(start: st, count: $0.count*channels)) 
    } 
} 

は、ラインlet st = UnsafeMutablePointer<UInt8>($0.baseAddress)このエラーをスロー

このエラーを解決するにはどうすればよいですか?

+0

[Swift 3 UnsafePointer($ 0)はXcode 8 beta 6でコンパイルされなくなる可能性があります](http://stackoverflow.com/questions/39046377/swift-3-unsafepointer0-no-longer-compile-in-xcode) -8-beta-6) – kennytm

+0

@kennytmその重複はありません。 – xaled

答えて

1

あなたは(すべてのプロパティのためにここにダミーの値を使用して)、UnsafeMutableRawBufferPointerUnsafeMutableBufferPointer<UInt8>を変換UInt8にそれを結合して、そのからUnsafeMutableBufferPointer<UInt8>を作成することができます。

final public class SGLImageRGBA8 : SGLImageType { 
    public var width: Int = 640 
    public var height: Int = 480 
    public var channels: Int = 4 
    public var rowsize: Int = 80 
    public var array:[(r:UInt8,g:UInt8,b:UInt8,a:UInt8)] = 
     [(r:UInt8(1), g:UInt8(2), b:UInt8(3), a:UInt8(4))] 

    public func withUnsafeMutableBufferPointer(body: @escaping (UnsafeMutableBufferPointer<UInt8>) throws -> Void) rethrows { 
     try array.withUnsafeMutableBufferPointer(){ bp in 
      let rbp = UnsafeMutableRawBufferPointer(bp) 
      let p = rbp.baseAddress!.bindMemory(to: UInt8.self, capacity: rbp.count) 
      try body(UnsafeMutableBufferPointer(start: p, count: rbp.count)) 
     } 
    } 
} 

SGLImageRGBA8().withUnsafeMutableBufferPointer { (p: UnsafeMutableBufferPointer<UInt8>) in 
    print(p, p.count) 
    p.forEach { print($0) } 
} 

プリント

UnsafeMutableBufferPointer(start: 0x000000010300e100, count: 4) 4 
1, 2, 3, 4 

参照してください。詳細については、UnsafeRawPointer MigrationおよびSE-0107を参照してください。

関連する問題