gpt4 book ai didi

ios - 无法将我的 Swift 类访问到 Objective-C 应用程序中

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:01 25 4
gpt4 key购买 nike

我已经在 Objective-C 中创建了一个应用程序。我想将用 Swift 编写的我的类用于应用程序,为此我做了以下事情

  • Defines Module设置为YES。
  • 定义产品模块名称 enter image description here

我创建了一个像这样的 swift 文件 MyClass.swift

import Foundation
class MyClass: NSObject
{
override init() {
super.init()
print("SwiftClass init")
}
}


@objc class SwiftClass : NSObject {

override init() {
super.init()
print("SwiftClass init")
}

func sayHello() -> Void {
print("hello");
}

func addX(x:Int, andY y:Int) -> Int {
return x+y
}

// Make a dictionary
// No, this code doesn't protect against values.count > keys.count
func dictionaryWithKeys(keys:[String], andValues values:[String]) -> Dictionary<String,String> {

var dictionary = Dictionary<String,String>()

for var i = 0; i < keys.count; i++ {
dictionary[keys[i]] = values[i]
}

return dictionary

}

}

成功清理和构建后,我的应用程序中有一个名为 Testing-Swift.h 的 swift 头文件,如图所示 - enter image description here

我可以在我的 ViewController.m 中导入我的头文件,但无法访问用 swift 编写的类 - enter image description here

请帮助我。我正在使用 Xcode 7.2

最佳答案

单击您的 Xcode 项目文件,单击build设置,找到一个搜索栏并搜索“定义模块”并勾选"is"。搜索“PRODUCT MODULE”后,写下你的项目名称,在你的应用程序委托(delegate)中重写#Import“YourProjectName-swift.h”

关于ios - 无法将我的 Swift 类访问到 Objective-C 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com