Inherit from a Swift class in Objective C (Objective-C继承Swift类)

·  阅读 907

搜到的相关资料基本来自这个帖子**Inherit from a Swift class in Objective C 。一楼回复已经说的很明确了,Objective-C不能继承Swift类**。

Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs: You cannot subclass a Swift class in Objective-C.

那么换个思路,为什么一定要用继承呢?用category(Swift Extension)可以吗?经过我的测试是可以的,代码如下:

Swift文件

import UIKit

@objcMembers  open class TestModel: NSObject {
    var testName: String = String()
}
复制代码

OC的.h文件

#import <Foundation/Foundation.h>
// 桥接文件
#import "****-Swift.h"


@interface TestModel (Add)
- (void)configTestName;
@end

复制代码

OC的.m文件

#import "TestModel+Add.h"

@implementation TestModel (Add)

- (void)configTestName {
    self.testName = @"12323";
}

@end
复制代码

以上代码,Build Successded

分类:
iOS
标签: