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

250
Views
Pasar un protocolo Swift a un puntero Objective-C

Usando XCode 10.1 / Swift 4.2.

Estoy tratando de asignar un objeto que se ajusta a un protocolo Swift a un puntero de Objective-C. El siguiente código es un ejemplo mínimo que compila y funciona como se esperaba, pero me da las siguientes advertencias:

Si se asigna a una variable local: Incompatible pointer types initializing 'NSObject<Animal> *__strong' with an expression of type 'id<Animal> _Nullable'

Si se asigna a una propiedad almacenada: Incompatible pointer types assigning to 'NSObject<Animal> *' from 'id<Animal> _Nullable'

¿Alguna idea sobre cómo abordar esa advertencia sin solo silenciarla?

Código SWIFT:

 @objc protocol Animal { var name: String { get } } @objc class Pig: NSObject, Animal { var name: String = "pig" } @objc class Cow: NSObject, Animal { var name: String = "cow" } @objc class Farm: NSObject { static func getAnimal(name: String) -> Animal? { // return some animal or nil } }

Código de Objective-C:

 // This code returns a valid pointer to a Pig object // that is usable in objective-c, but it also triggers // the warning described above NSObject<Animal>* animal = [Farm getAnimalWithName:@"pig"];
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Especifique que cada implementador Animal también implemente la interfaz de NSObject : @objc protocol Animal : NSObjectProtocol

También puede cambiar el tipo de variable en ObjC a id<Animal> .

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!