Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Puente automático de funciones de estructura C a métodos Swift

¿Existe una convención de nomenclatura que permita a Swift asociar automáticamente las funciones de C que actúan en una estructura con la estructura misma?

Por ejemplo, CGRect se declara de la siguiente manera:

 struct CGRect { CGPoint origin; CGSize size; }; typedef struct CG_BOXABLE CGRect CGRect; ... CG_EXTERN CGFloat CGRectGetMinX(CGRect rect) CG_AVAILABLE_STARTING(10.0, 2.0);

Pero puedo escribir código Swift como este:

 let rect = CGRect() let minX = rect.minX

Intenté copiar lo mismo con la esperanza de poder obtener el mismo comportamiento de mis propias estructuras C, pero parece que no puedo hacer que funcione correctamente.

¿Este comportamiento automático se debe exactamente a cómo se declara la estructura, o se agrega en otro lugar a través de extensiones/alguna otra magia? Si es a través de extensiones, ¿cómo se deshabilita la función CGRectGetMinX original en Swift?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Swift 3 introdujo la función para importar funciones globales como funciones miembro, consulte SE-0044 Importar como miembro .

Aquí hay un ejemplo simple de cómo puede usar eso para sus propios tipos de estructuras:

 typedef struct MyRect { int x; int y; int w; int h; } MyRect; // Import as computed property: int MyRectGetMinX(MyRect rect) __attribute__((swift_name("getter:MyRect.minX(self:)"))); // Import as method: int MyRectArea(MyRect rect) __attribute__((swift_name("MyRect.area(self:)")));

Ahora puedes llamarlo desde Swift como

 let rect = MyRect() let mx = rect.minX let ar = rect.area()

y el uso de la función global no se compila:

 let mx = MyRectGetMinX(rect) // Error: 'MyRectGetMinX' has been replaced by property 'MyRect.minX' let ar = MyRectArea(rect) // Error: 'MyRectArea' has been replaced by instance method 'MyRect.area()'
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!