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