Type ID in swift
-
I often see the type code in lessons and examples
+ (id)parameterUnitWithObject:(id)object{
And I can't figure out what this is all about id?
-
id
- an index for any type, but different fromvoid *
He always points to the object Objective-C. In other words, a type objectid
should be inherited fromNSObject
orNSProxy
(or simply having all the methods of these classes -retain
♪release
♪isa
and so on.The compiler will normally accept the caste of any ObjC facility in id and vice id in any objective-c. This principle is based on all (or almost all) types of containers in ObjC.
Another interesting feature is that you can do any known compiler technique on the Oid site. For example:
id something; [something becomeFirstResponder];
To what is the equivalent of id in swift:
By a swift equivalent
id
TypeAnyObject
orAnyObject?
if it could be nil.For example:
var something: AnyObject
or
@IBAction func buttonClicked(sender : AnyObject) { print("Button was clicked", sender) }
or
func newFunc() -> AnyObject? { return nil }
or
let something = "something" self.someMethod(something as AnyObject)