详细信息: Xcode 8.1中带有Swift 3代码的Objective-C项目
任务:
- 在Objective-C类中使用Swift枚举
 
- 在Swift类中使用Objective-C枚举
 
完整样本
1.使用Swift枚举的Objective-C类
对象类
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, ObjcEnum) {
    ObjcEnumValue1,
    ObjcEnumValue2,
    ObjcEnumValue3
};
@interface ObjcClass : NSObject
+ (void) PrintEnumValues;
@end
对象类
#import "ObjcClass.h"
#import "SwiftCode.h"
@implementation ObjcClass
+ (void) PrintEnumValues {
    [self PrintEnumValue:SwiftEnumValue1];
    [self PrintEnumValue:SwiftEnumValue2];
    [self PrintEnumValue:SwiftEnumValue3];
}
+ (void) PrintEnumValue:(SwiftEnum) value {
    switch (value) {
        case SwiftEnumValue1:
            NSLog(@"-- SwiftEnum: SwiftEnumValue1");
            break;
            
        case SwiftEnumValue2:
        case SwiftEnumValue3:
            NSLog(@"-- SwiftEnum: long value = %ld", (long)value);
            break;
    }
}
@end
在Objective-C代码中检测Swift代码
在我的示例中,我使用SwiftCode.h在Objective-C中检测Swift代码。该文件自动生成(我没有在项目中创建此头文件的物理副本),并且您只能设置该文件的名称:


如果编译器找不到您的头文件Swift代码,请尝试编译项目。
2.使用Objective-C枚举的Swift类
import Foundation
@objc
enum SwiftEnum: Int {
    case Value1, Value2, Value3
}
@objc
class SwiftClass: NSObject {
    
    class func PrintEnumValues() {
        PrintEnumValue(.Value1)
        PrintEnumValue(.Value2)
        PrintEnumValue(.Value3)
    }
    
    class func PrintEnumValue(value: ObjcEnum) {
        switch value {
        case .Value1, .Value2:
            NSLog("-- ObjcEnum: int value = \(value.rawValue)")
            
        case .Value3:
            NSLog("-- ObjcEnum: Value3")
            break
        }
        
    }
}
在Swift代码中检测Objective-C代码
您需要创建桥接头文件。当您在Objective-C项目中添加Swift文件或在Swift项目中的Objective-C文件中时,Xcode会建议您创建桥接标头。

您可以在此处更改桥接头文件名:

桥接头
#import "ObjcClass.h"
用法
#import "SwiftCode.h"
...
[ObjcClass PrintEnumValues];
[SwiftClass PrintEnumValues];
[SwiftClass PrintEnumValue:ObjcEnumValue3];
结果

更多样品
上面描述的完全集成步骤Objective-c和Swift。现在,我将编写其他一些代码示例。
3.从Objective-c代码调用Swift类
斯威夫特班
import Foundation
@objc
class SwiftClass:NSObject {
    
    private var _stringValue: String
    var stringValue: String {
        get {
            print("SwiftClass get stringValue")
            return _stringValue
        }
        set {
            print("SwiftClass set stringValue = \(newValue)")
            _stringValue = newValue
        }
    }
    
    init (stringValue: String) {
        print("SwiftClass init(String)")
        _stringValue = stringValue
    }
    
    func printValue() {
        print("SwiftClass printValue()")
        print("stringValue = \(_stringValue)")
    }
    
}
Objective-C代码(调用代码)
SwiftClass *obj = [[SwiftClass alloc] initWithStringValue: @"Hello World!"];
[obj printValue];
NSString * str = obj.stringValue;
obj.stringValue = @"HeLLo wOrLd!!!";
结果

4.从Swift代码调用Objective-c类
Objective-C类(ObjcClass.h)
#import <Foundation/Foundation.h>
@interface ObjcClass : NSObject
@property NSString* stringValue;
- (instancetype) initWithStringValue:(NSString*)stringValue;
- (void) printValue;
@end
对象类
#import "ObjcClass.h"
@interface ObjcClass()
@property NSString* strValue;
@end
@implementation ObjcClass
- (instancetype) initWithStringValue:(NSString*)stringValue {
    NSLog(@"ObjcClass initWithStringValue");
    _strValue = stringValue;
    return self;
}
- (void) printValue {
    NSLog(@"ObjcClass printValue");
    NSLog(@"stringValue = %@", _strValue);
}
- (NSString*) stringValue {
    NSLog(@"ObjcClass get stringValue");
    return _strValue;
}
- (void) setStringValue:(NSString*)newValue {
    NSLog(@"ObjcClass set stringValue = %@", newValue);
    _strValue = newValue;
}
@end
Swift代码(调用代码)
if let obj = ObjcClass(stringValue:  "Hello World!") {
    obj.printValue()
    let str = obj.stringValue;
    obj.stringValue = "HeLLo wOrLd!!!";
}
结果

5.在Objective-c代码中使用Swift扩展
迅捷扩展
extension UIView {
    static func swiftExtensionFunc() {
        NSLog("UIView swiftExtensionFunc")
    }
}
Objective-C代码(调用代码)
[UIView swiftExtensionFunc];
6.在快速代码中使用Objective-c扩展
Objective-C扩展(UIViewExtension.h)
#import <UIKit/UIKit.h>
@interface UIView (ObjcAdditions)
+ (void)objcExtensionFunc;
@end
UIViewExtension.m
@implementation UIView (ObjcAdditions)
+ (void)objcExtensionFunc {
    NSLog(@"UIView objcExtensionFunc");
}
@end
Swift代码(调用代码)
UIView.objcExtensionFunc()
               
              
YourProjectName-Swift.h应该是Xcode在编译过程中自动为您创建的一个神奇的头文件(您实际上不会在项目浏览器中看到它)。尝试删除您创建的类,然后将其添加#import YourProjectName-Swift.h到要使用Swift类的文件中。