What is the syntax for casting a reference to a variable in Swift? -


being new swift (and having issues swift's pointer handling) i'm unable parse errors xcode gives me. tips on getting reference variable?

rgba just struct of 4 uint8s

var data: unsafemutablepointer<rgba> = unsafemutablepointer<rgba>(cgbitmapcontextgetdata(bitmapcontext)) var pixel = &data[offset] // [1] trouble statement  // use pixel struct read *and* write 

[1] errors

  • 'myclass.rgba' not convertible 'unsafemutablepointer'
  • type 'inout myclass.rgba' of variable not materializable

how come able access memory using subscript operator (data[offset].r = 0) have use memory property after setting element reference (var pixel = data[offset])? surely there must swift way storing reference in local?

i can accomplish same through inout parameter function, ie;

func setpixel(inout pixel: rgba, var r: uint8, var g: uint8, var b: uint8, var a: uint8) {   pixel.r = r // can use pixel directly without '.memory' } 

you can add offset pointer:

let data = unsafemutablepointer<rgba>(cgbitmapcontextgetdata(bitmapcontext)) let pixel = data + offset 

then can read/write memory pointed pixel:

let red = pixel.memory.red pixel.memory.blue = 123 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -