Swift与Objective-C的兼容能力容许你在同一个工程中同时使用两种语言。你能够用这种叫作“mix and match”的特性来开发基于混合语言的应用。使用Swfit的最新特性--“mix and match”,你能够实现应用的一部分功能,并没有缝地并入已有的Objective-C的代码中。html
#import "XYZCustomCell.h" #import "XYZCustomView.h" #import "XYZCustomViewController.h"
let myCell = XYZCustomCell() myCell.subtitle = "A custom cell"
#import “ProductModuleName-Swift.h”
target 中任何 Swift 文件将会对 Objc .m 文件可见,包括这个 import 语句。关于在 Objc 代码中使用 Swift 代码,详见 Using Swift from Objective-C。ios
#import <XYZ/XYZCustomCell.h> #import <XYZ/XYZCustomView.h> #import <XYZ/XYZCustomViewController.h>
let myCell = XYZCustomCell() myCell.subtitle = "A custom cell"
#import <ProductName/ProductModuleName-Swift.h>
这个 import 语句所包含的 Swift 文件均可以被同个框架 target 下的 Objc .m 源文件访问。关于在 Objc 代码中使用 Swift 代码,详见 Using Swift from Objective-C。git
import FrameworkName
@import FrameworkName;
MySwiftClass *swiftObject = [[MySwiftClass alloc] init]; [swiftObject swiftMethod];
// MyObjcClass.h @class MySwiftClass; @interface MyObjcClass : NSObject - (MySwiftClass *)returnSwiftObject; /* ... */ @end
http://www.cocoachina.com/newbie/basic/2014/0605/8688.htmlgithub