Created
June 8, 2015 12:06
-
-
Save mgrebenets/96a0f3f26512ffba5ab1 to your computer and use it in GitHub Desktop.
Swift Objective-C Interoperability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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