Last active
April 17, 2019 14:23
-
-
Save mofarajmandi/fced89cc0c37ee1429513e1c191e667a to your computer and use it in GitHub Desktop.
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 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