Skip to content

Instantly share code, notes, and snippets.

@mofarajmandi
Last active April 17, 2019 14:23
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 mofarajmandi/fced89cc0c37ee1429513e1c191e667a to your computer and use it in GitHub Desktop.
Save mofarajmandi/fced89cc0c37ee1429513e1c191e667a to your computer and use it in GitHub Desktop.
import Foundation
protocol SwiftProtocol {
func passAnyAround(_ input: Any?)
}
@objc public protocol ObjcProtocol {
func passAnyAround(_ input: Any?)
}
class SwiftClassA: SwiftProtocol {
func passAnyAround(_ input: Any?) {
print("---------------")
dump(input)
let isBool = input is Bool
print(isBool)
let isInt = input is Int
print(isInt)
print(type(of: input))
print("---------------")
}
}
@objc class ObjcClassA: NSObject, ObjcProtocol {
@objc func passAnyAround(_ input: Any?) {
print("---------------")
dump(input)
let isBool = input is Bool
print(isBool)
let isInt = input is Int
print(isInt)
print(type(of: input))
print("---------------")
}
}
let boolValue = false
// retains the Bool type
let swiftA: SwiftProtocol = SwiftClassA()
swiftA.passAnyAround(boolValue)
// This version earases the type
let objcA: ObjcProtocol = ObjcClassA()
objcA.passAnyAround(boolValue)
// version2 (not declaring the object as of type ObjcProtocol) retains the Bool type
let objcB = ObjcClassA()
objcB.passAnyAround(boolValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment