2

Hello I want to use this method inside my swift class, already create the header, but I only manage to access the method (setApiKey). this is the code in objective c

#import <Foundation/Foundation.h>

@class CLQResponseHeaders, CLQError;
@class CLQToken;

NS_ASSUME_NONNULL_BEGIN
@interface Culqi : NSObject

/**
 * gets singleton object.
 * @return singleton
 */
+ (Culqi *_Nonnull)sharedInstance;

+ (void)setApiKey:(NSString *_Nonnull)apiKey;

#pragma mark - Tokens

- (void)createTokenWithCardNumber:(NSString *_Nonnull)cardNumber
                              cvv:(NSString *_Nonnull)cvv
                  expirationMonth:(NSString *_Nonnull)expirationMonth
                   expirationYear:(NSString *_Nonnull)expirationYear
                            email:(NSString *_Nonnull)email
                         metadata:(NSDictionary * _Nullable)metadata
                          success:(void (^_Nullable)(CLQResponseHeaders *_Nonnull responseHeaders, CLQToken * _Nonnull token))success
                          failure:(void (^_Nullable)(CLQResponseHeaders *_Nonnull responseHeaders, CLQError * _Nonnull businessError, NSError * _Nonnull error))failure;

@end
NS_ASSUME_NONNULL_END

My swift code is

import Foundation
import UIKit

class RegisterController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //THIS METHOD CALL SUCCESS
        Culqi.setApiKey("")


    }

}

Update: the method setApiKey is accessible, what happens is that I can not call the method "createTokenWithCardNumber"

Krunal
  • 77,632
  • 48
  • 245
  • 261
Dac Devs
  • 21
  • 2
  • 3
  • 1
    Possible duplicate of [How to call Objective-C code from Swift](https://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift) – Clonkex Oct 03 '17 at 03:11
  • If my class is accessible, what happens is that I can not access this method (createTokenWithCardNumber) – Dac Devs Oct 03 '17 at 03:49
  • @DacDevs I think, this is your Imlementation (.m) file code. right? You need to move method declaration to (.h) header file. – Krunal Oct 03 '17 at 03:57
  • Yes in my implementation file is right, this code is in objective-c for execute the method, but in swift not call `[[Culqi sharedInstance] createTokenWithCardNumber: }];` – Dac Devs Oct 03 '17 at 04:26
  • The documentation is here, https://github.com/culqi/culqi-ios – Dac Devs Oct 03 '17 at 04:28
  • Try this: Culqi.sharedInstance().createTokenWithCardNumber(##) – Krunal Oct 03 '17 at 05:59
  • You need to import Culqi modul in you controller before calling any function. Write @import Culqi in RegisterController and after that you can call Culqi function. – Pawan Rai Oct 03 '17 at 06:18
  • @DacDevs I am facing the same problem, my swift instance methods declared with objc is not visible in Objective C file. have you find any solution to this problem ? – Stunner Jul 24 '22 at 23:14

1 Answers1

2

Declare your function/method setApiKey in header file of your Objective-C class.

// Move following line/code to header file (.h) of your objective-c class
+ (void)setApiKey:(NSString *_Nonnull)apiKey;

Your method createTokenWithCardNumber is an instance method (not a class method), you need to create an instance of your class to access it. Try this,

let c = Culqi()
c.createTokenWithCardNumber(#<your parameter arguments>#)

// or use shared instance
Culqi.sharedInstance().createTokenWithCardNumber(#<your parameter arguments>#)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • thanks for your answer, but I have already created the Bridging-Header.h file and are importing the class correctly – Dac Devs Oct 03 '17 at 03:32
  • the method setApiKey is accessible, what happens is that I can not call the method "createTokenWithCardNumber" – Dac Devs Oct 03 '17 at 04:00
  • @DacDevs what was the corrected class? could you share? – Edu Mar 15 '19 at 05:52