oc 继承 swift 类

oc 继承 swift 类

18年时的文章,内容过期。

项目中使用 oc 继承 swift 类编译器报错:

Cannot subclass a class that was declared with the 'objc_subclassing_restricted' attribute

oc 继承前增加 objc_subclassing_restricted 注释,实现对 swift 类的继承。

Swift 类:

import Foundation

class SwiftClass : NSObject {
  func say() {
    print("hi");
  }
}

Objc 类:

#import 

#import "test-Swift.h"

#define SWIFT_SUBCLASS __attribute__((objc_subclassing_restricted))

SWIFT_SUBCLASS
@interface ObjcClass : SwiftClass
- (instancetype)init;
@end
@implementation ObjcClass
- (void)say {
  NSLog(@"oops");
}
@end

原文链接

你可能感兴趣的:(oc 继承 swift 类)