简体   繁体   中英

Unknown type name 'Tester' when trying to use swift class inside objective-c file

I am trying to use a swift class in a an objective-c file, but I am met with the error Unknown type name 'Tester' below is a snippet of my swift code:

import Foundation

class Tester{
  var studentname: String = ""
  var mark: Int = 0
  var mark2: Int = 0
}

and here is my objective-c code that is intended to use the swift class:

#import "Stringator.h"
#import "reactnativeios-Swift.h"


@interface Stringator()
@property (nonatomic, strong) Tester *tester;
@end


@implementation Stringator

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(greetings:(RCTResponseSenderBlock)callback){

  NSString* someString = _tester.studentname;

  callback(@[someString]);
}

@end

According to the error I get, it seems that objective-c cannot find my Tester class, how do I solve this issue?

Add @objc before class name and make it subclass of NSObject . Hence it will visible on Objective-C. After changing your code clean build folder so it will re-generate reactnativeios-Swift.h .

import Foundation

@objc class Tester: NSObject
{
  @objc var studentname: String = ""
  @objc var mark: Int = 0
  @objc var mark2: Int = 0
}

You can access Tester and as below,

Tester *tester = [[Tester alloc]init];
tester.studentname = @"John";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM