我用Swift写了一个库,但我不能把它导入到我目前用Objective-C写的项目中。

有什么方法可以导入吗?

#import "SCLAlertView.swift" - 'SCLAlertView.swift' file not found

当前回答

在同一个项目中检查Swift和Objective C的发布前说明

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75

你应该导入

#import "SCLAlertView-Swift.h"

其他回答

第一步:-

选择项目目标->构建设置->搜索(“定义”)->定义模块 将值从No更新为Yes

“定义模块”:YES。

“总是嵌入Swift标准库”:是的。

“安装Objective-C兼容头”:YES。

第二步:-

在Objective C“.h”文件中添加Swift文件类,如下所示

#import <UIKit/UIKit.h>

@class TestViewController(Swift File);

@interface TestViewController(Objective C File) : UIViewController

@end

在Objective C中导入“ProjectName(你的项目名)-Swift.h”。m”文件

//TestViewController.m 
#import "TestViewController.h"

/*import ProjectName-Swift.h file to access Swift file here*/

#import "ProjectName-Swift.h"

下面是该怎么做:

在Objective-C中创建一个新项目 创建一个新的.swift文件   将会出现一个弹出窗口并询问“您是否想配置一个Objective-C桥接头”。 选择Yes。 点击Xcode项目文件 点击构建设置 找到搜索栏并搜索定义模块。 将“value”修改为“Yes”。 搜索“产品模块名称”。 将该值更改为项目的名称。 在App委托中,添加以下内容:


注意:无论何时你想使用你的Swift文件,你必须导入以下行:

#进口“YourProjectName-Swift.h”

#import <TargetName-Swift.h>

当你从键盘输入#import <时,你会看到Xcode会自动提示你。

在项目中找到. pch文件。然后添加#import "YourProjectName-Swift.h"这将导入类头文件。这样你就不需要导入到特定的文件中。

#ifndef __IPHONE_3_0
#warning "This project uses features only available in iPhone SDK 3.0 and later."
#endif


#ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    #import "YourProjectName-Swift.h"
#endif

如果你在Swift 4中创建了一个项目,然后添加了Objective-C文件,这样做:

@objcMembers
public class MyModel: NSObject {
    var someFlag = false         
    func doSomething() {         
        print("doing something")
    }
}

参考:https://useyourloaf.com/blog/objc-warnings-upgrading-to-swift-4/