Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

829
Visualizações
Syntax to Call Swift Method from Objective-C Class

I am trying to call some code in a Utilities file written in swift from an objective-c file.

Right now the class in the swift file looks like this:

class ImageClass: NSObject {
public func imageFromString(name: String?) -> UIImage? {
//code to make image

return image
}
}

I am trying to call it from the Objective-C file with:

 UIImage *myImage = [ImageClass imageFromString:@"test"];

however this is giving error:

Use of undeclared identifier 'ImageClass'

I know the Utilities file is reachable from the Objective-C file because I call other methods there though using a shared Instance.

Can anyone suggest the proper way to call the code? I actually don't care if it's a class or through a sharedInstance, I just need to be able to call it.

Edit:

As suggested by @Charles Srstka, I CMD-clicked on the #import myapp.swift file, jumped to Definition, and it shows the following in the header:

- (UIImage * _Nullable)imageFromStringWithName:(NSString * _Nullable)name SWIFT_WARN_UNUSED_RESULT;

Accordingly, I tried adding WithName (which I'd forgotten) to the invocation so I have:

 UIImage *myImage = [ImageClass imageFromStringWithName:@"test"];

but am still getting error with or without @objc before method:

No known class method for selector 'imageFromStringWithName:'
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In swift 4.2 and latest

  1. Add this line to your "*.m" file:

    #import "YourProjectName-Swift.h"
    
  2. Add @objc before your function:

    @objc public func imageFromString(name: String?) -> UIImage? {    
    //code to make the image
    return image
    }
    
  3. Call it inside Objective-C class:

    ImageClass *imgObject = [[ImageClass alloc] init];   
    UIImage *myImage = [imgObject imageFromStringWithName:@"test"];
    
over 4 years ago · Santiago Trujillo Relatório

0

Add this line to your Objective-C file:

#import "YourProjectName-Swift.h"

Replace YourProjectName with the name of your project. This will make all the Objective-C-compatible Swift classes in your project visible to your Objective-C code.

Note that you'll also need to add the @objc annotation to your func declataions to make the methods visible to Objective-C.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda