Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vprtwn/1b93825284d959435b64 to your computer and use it in GitHub Desktop.
Save vprtwn/1b93825284d959435b64 to your computer and use it in GitHub Desktop.

Integrating Swift With Objective-C

Introduction

  • Swift is modern, type-safe, expressive, performant

  • but Objective-C remains a first-class citizen

  • Same design patterns, Cocoa APIs

  • "We do not want you to rewrite or stop improving your existing code!"

  • Should you use unowned or weak for delegates?

  • To expose Objective-C to Swift, use a bridging header

    • MyApp-Bridging-Header.h
  • To expose Swift to Objective-C, Xcode gives you a generated header

    • Generated as part of build process
    • #import "MyApp-Swift.h"
  • In a framework target, everything in the umbrella header is exposed to Swift (no bridging header)

init(data: NSData, type: String) {
	self.data = data.copy() as NSData
	// copy returns an `AnyObject` so we nee`d to cast it
	self.type = type
}

init(coder aDecoder: NSCoder) {
	data = aDecoder.decodeObjectForKey("data") as NSData
	type = aDecoder.decodeObjectForKey("type") as NSString
}

@import

  • If a framework was built with Xcode 6 and has the defined modules build setting turned on, you should use @import
  • Not sure how frameworks play out with cocoapods?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment