WWDC 2014 Notes

Session 407

id and AnyObject

  • Runtime Check


if([object respondsToSelector:@selector(removeFormSuperview)]){
	[object removeFromSuperview];


let object:AnyObject = SomeClass()
  • Downcasting AnyObject
    • AnyObject does not implicity downcast
      let view:UIView = object //error:'AnyObject' cannot be implicitly downcast
    • 使用”as”在运行时做类型转化
      let view = object as UIView
    • 使用”as?”
      if let view = object as? UIView{
          // view is UIView

Implicitly Unwrapped Optionals

  • From WWDC session 407:
    • A value of class type in Swift is never nil
    • Optional types generalize the notion of nil
    • Objective-C does not have a notion of a “never nil” pointer
    • ”!” is an implicitly unwrapped optional
    • Can be tested explicity for nil
    • Can directly access properties/methods of the underlying value
    • Can be implicity converted to its underlying value(e.g. NSDate)
  • 做成员变量:

var date:NSDate!意思是,当使用date的时候,会隐式的unwrap,例如,当用self.date的时候,相当于self.date!。如果datenil,那么会crash。

  • 做参数:


- (NSString* )type:(NSString* )name operation:(UIDocumentSaveOperation)op;
func type(name:String!, operation: UIDocumentSaveOperation) -> String!


  • Objective-C
@protocol UITableViewDataSource<NSObject>
- (NSInteger)numberOfSectionsInTableView:(UITableView)tableview;
- (NSInteger)tableView:(UITableView* )tableView numberOfRowsInSection:(NSInteger)section;

@property id<UITableViewDataSource> dataSource;
  • Swift
@objc protocol UITableViewDataSource:NSObjectProtocol{
	func tableView(tableView:UITableView, numberOfRowsInSection: Int) -> Int
	@optional func numberOfSectionsInTableView(tableView:UITableView) -> Int
var dataSource: UITableViewDataSource!
  • Downcast via as? to protocol type
if let ds = object as? UITableViewDataSource{
	let rowsInFirstSection = ds.tableView(tableView,numberOfRowsInSection:0)

Swift Objects are the OC objects

All Swift classes are “id compatible”

  • Same layout as an Objective-C class
  • Same basic infrastructure(retain/release/class/etc.) inheriate from Objective-C class to make your class directly visible in Objective-c, 继承Objective-C class使其能够被OC运行时发现

getters and setters

var name:String{
	get { return "b" }
	set {}

Override the property itself not the getter or setter

  • getter:
override var name:String{
	return "this is my \(self.view) string"


“objc” attribute verifies that the declaration can be used in Objective-C。 objc关键字的作用本质上是让Swift中的某个数据结构(类,方法等)在OC的运行时可见。例如,对于Swift中返回tuple的函数,对于这种类型的Swift函数,编译器会给出错误。

@objc func myGenericMethod<T>(x:T) -> (String,String){...} //error: not expressible in oc


var enabled: Bool{


@objc(ABCMyDocument) class MyDocument:UIDocument{


Unmanaged Object

Unmanaged enables manual memory management

struct Unmanaged<T:AnyObject>{

	func takeUnretainedValue() -> T
	func takeRetainedValue() -> T

Use it to work with unaudited CF APIs

let color = CGColorGetRandomColor().takeUnretainedValue()