By the default we should use "Any" and if we need more precision then we can chose more constrained types like AnyHashable, AnyObject, AnyClass, CustomType etc.
AnyObject should be used when sanding an object to frameworks which were written in Objective-C like UIKit and Foundation.
AnyObject accepts an instance of a class types and instances of types like enum, struct and optional class types objects are not accepted:
class CustomType {
var instanceArg1: Int
var instanceArg2: String
static var classArg1: Int
}
Particularity of the AnyObject type is that an object of this type has access to all @objc methods and variables by autocompletion. If UIKit or Foundation are imported then the AnyObject instance object has access to all variables and methods of all Foundation objects by autocompletion.
the code below, compiles but crashes the app in runtime first of all due to the fact that given argument is of type String and not of UITableView and there is no a method tableView(_:numberOfRowsInSection) defined in String type.
import UIKit
class CustomType {
func customMethod(arg1: AnyObject) {
let tableView = UITableView()
let numberOfRows = arg1.tableView(tableView, numberOfRowsInSection: 0)
}
}
String
,Array
andDictionary
are not classes, for these use Any. – Philosophism