改动main.m文件
#importOC文件:Root.h#import "Root.h"int main(int argc, const char * argv[]){ @autoreleasepool { Root *rt = [[Root alloc] init]; [rt desc]; } return 0;}
#importRoot.m//Objective-c 的头文件假设须要引用Swift的类。则能够使用以下这样的方式@class Person;@interface Root : NSObject-(Person *)returnPerson;-(void)desc;@end
#import "Root.h"//Objective-c调用Swift//须要导入固定的头文件。此头文件项目里面找不到,但却是存在。
而且会自己主动把Swift类转换成OC的类,在里面能找到 //格式为 #ProductName#-Swift.h #import <OC_Swift-Swift.h> @implementation Root -(void)desc { Person *ps = [[Person alloc] initWithName:@"Rose"]; ps.name = @"Jack"; [ps desc]; } -(Person *)returnPerson { Person *ps = [[Person alloc] initWithName:@"Tom"]; return ps; } @end
Swift文件:Person.swiftimport Foundation//假设此类须要被OC的类来调用。一定要继承自NSObjectclass Person : NSObject{ var name: String { willSet { NSLog("将要把名字设置为:" + name) } } override init() { self.name = "" } init(name: String) { self.name = name } func desc() { print("这是一个Swift的类,name: " + self.name) }}