尝试在Objective-C文件中使用swift类时未知类型名称“ Tester”

时间:2019-11-26 09:46:41

标签: ios objective-c swift

我正在尝试在Objective-C文件中使用swift类,但是下面的错误<style> .onlineStatus { height: 20px; width: 20px; background-color: green; border-radius: 50%; display: inline-block; } </style> <span class="onlineStatus" id="onlineStatus"></span> Online <script> var div = document.getElementById('onlineStatus'); div.style.backgroundColor = 'green'; var today = new Date(); var hr = today.getHours(); if(hr <= 9 && hr >= 17 || today.getDay() == 6 || today.getDay() == 0) div.style.backgroundColor = 'red'; </script>是我的swift代码的摘要:

Unknown type name 'Tester'

这是我打算使用swift类的Objective-C代码:

import Foundation

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

根据我得到的错误,看来Objective-C找不到我的Tester类,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在类名之前添加@objc,并将其作为NSObject的子类。因此,它将在Objective-C上可见。更改代码后,清理构建文件夹,以便它将重新生成reactnativeios-Swift.h

import Foundation

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

您可以访问Tester,如下所示,

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