【发布时间】:2016-05-20 00:14:12
【问题描述】:
我正在尝试将我的应用程序从 Objective C 迁移到 swift。 我有几个 Objective C 类别。由于 Swift 有“扩展”,我创建了它。但是有些方法,我无法从现有的 Objective C 中调用它。
以下代码给出编译错误
No visible @interface for 'NSString' declares the selector 'myCategory'
扩展类
import Foundation
extension String {
func myCategory() -> String {
return "I am From Swift";
}
}
我的视图控制器
#import <TestProject-Swift.h>
@interface TestViewController ()
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *str = @"Testing";
NSLog(@"Here is result %@", [str myCategory]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
它也将如何与 Swift 类一起工作? 我还为上述扩展文件创建了桥接头文件。
编辑: 我已经尝试过“公开”,但它不起作用。
【问题讨论】:
-
我已经尝试过但没有成功
标签: ios objective-c swift