Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var list:[Any] = ["A Random String", 12, 5.2, CGPoint.zero]
- // Since the list contains elements of type Any, we can append anything into it.
- list.append(UIColor.red)
- // Since type is Any, to use any of the elements we need to type check them:
- list.forEach { (element) in
- switch element {
- case is String:
- print("String detected: \(element)")
- case is Int:
- print("Int detected: \(element)")
- case is Double:
- print("Double detected: \(element)")
- case let point as CGPoint:
- print("frame detected: x:\(point.x) y:\(point.y)")
- default:
- print("Default: \(element)") /* will print the UIColor
- as this doesn't fall into any case */
- }
- }
Add Comment
Please, Sign In to add comment