Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
Swift: understanding pointers

I'm trying to create a CoreGraphics Context using the following MacOS Quartz API:

CGContext.init?(_ url: CFURL, 
                mediaBox: UnsafePointer<CGRect>?, 
                _ auxiliaryInfo: CFDictionary?)

, but I'm having trouble with the concept of pointers. I can initiate the context thus:

var mypointer: UnsafeMutablePointer<CGRect>!
mypointer.pointee = myCGRectangle
var writeContext = CGContext.init(outURL, mediaBox: mypointer, nil)

But it says that pointee is a get-only property. I've tried:

var mypointer: UnsafePointer<CGRect>! = UnsafePointer(myCGRectangle)

, based on this, but I get cannot convert CGRect to expected type, amongst other errors.

I have looked at several other questions here about pointers, but I can't find anything useful within them.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Just pass a reference to your myCGRectangle variable, this will create the needed UnsafeMutablePointer:

var writeContext = CGContext(outURL, mediaBox: &myCGRectangle, nil)

Make sure the rectangle variable is declared as var, otherwise the & won't work.

More details can be found on this excellent Apple documentation page about passing pointers to functions that require ones.

over 4 years ago · Santiago Trujillo Denunciar

0

Typically you do not just create a variable of type UnsafeMutablePointer<CGRect> and pass it to the init(_:mediaBox:_:). You rather pass a rectangle for which PDF will be displayed, e.g.

var mediaBox = CGRect(x: 0, y: 0, width: 300, height: 500)
...
var writeContext = CGContext(outURL, mediaBox: &mediaBox, nil)

(even that is a simplification, as it creates a rectangle directly. Usually you have some rectangle of the view you want to display PDF in, so you will be passing that rectangle.)

You are also allowed to pass nil to the init(_:mediaBox:_:). In which case, as docs say:

If you pass NULL, CGPDFContextCreateWithURL uses a default page size of 8.5 by 11 inches (612 by 792 points).

So if that rectangle fits your needs, just pass nil (simpler).

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda