Skip to content

Instantly share code, notes, and snippets.

@mgrebenets
Created June 8, 2015 12:06
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 mgrebenets/96a0f3f26512ffba5ab1 to your computer and use it in GitHub Desktop.
Save mgrebenets/96a0f3f26512ffba5ab1 to your computer and use it in GitHub Desktop.
Swift Objective-C Interoperability
import AppKit
typealias BaseObjCClass = NSObject
enum PureSwiftEnum {
case Value, AnotherValue
}
protocol PureSwiftProtocol {
var value: PureSwiftEnum { get }
var size: CGSize { get }
}
class ObjCSubclass: BaseObjCClass, PureSwiftProtocol {
var value: PureSwiftEnum = .Value
var size: CGSize = CGSizeZero
}
class GenericClass<T where T: PureSwiftProtocol> {
var node: T
init(node: T) {
self.node = node
}
func accessValue() -> PureSwiftEnum {
return node.value
}
func accessSize() -> CGSize {
return node.size
}
}
class PureSwiftWrapper: PureSwiftProtocol {
var objcInstance: ObjCSubclass
init(objcInstance: ObjCSubclass) {
self.objcInstance = objcInstance
}
var value: PureSwiftEnum {
return objcInstance.value
}
var size: CGSize {
return objcInstance.size
}
}
let wrapper = PureSwiftWrapper(objcInstance: ObjCSubclass())
let objectGeneric = GenericClass(node: ObjCSubclass())
let wrapperGeneric = GenericClass(node: wrapper)
println(objectGeneric.accessValue())
println(wrapperGeneric.accessValue())
println(objectGeneric.accessSize())
println(wrapperGeneric.accessSize())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment