2014-09-24 11 views
8

ho creato una struttura a Swift chiamato RGB, abbastanza semplice:Typecast UnsafeMutablePointer <Void> a UnsafeMutablePointer <#Struct tipo #>

struct PixelRGB { 
    var r: CUnsignedChar = 0 
    var g: CUnsignedChar = 0 
    var b: CUnsignedChar = 0 

    init(red: CUnsignedChar, green: CUnsignedChar, blue: CUnsignedChar) { 
     r = red 
     g = green 
     b = blue 
    } 
} 

E ho un puntatore var imageData: UnsafeMutablePointer<PixelRGB>!.

desidero malloc po 'di spazio per questo puntatore, ma malloc rendimenti UnsafeMutablePointer<Void> e non riesco a lanciarla come di seguito:

imageData = malloc(UInt(dataLength)) as UnsafeMutablePointer<PixelRGB> // 'Void' is not identical to `PixelRGB` 

Comunque per risolvere questo problema? Grazie per l'aiuto!

+2

Come su 'imageData = UnsafeMutablePointer .alloc (dataLength)'? – matt

+0

@matt Questa dovrebbe essere una risposta. –

+0

Okey-dokey, lo farà. – matt

risposta

19

Penso che ciò che si vuole dire è qualcosa di simile:

imageData = UnsafeMutablePointer<PixelRGB>.alloc(dataLength) 
+0

E come liberarlo? –

+0

@AntonHolmquist imageData.destroy() –

+0

@AntonHolmquist L'opposto di alloc() è dealloc(). Il metodo destroy() è la controparte di initialize(). – eofster

Problemi correlati