I'm receiving an event that passes a parameter of type ()
.
Is it an empty tuple..?
I'm referring to this type of expression:
let x = ()
NSCopying can be used in Swift as a generic way to create copies of classes (reference types), and as a bonus, making a Swift type inherit from NSCopying will also allow it to make use of the copy property accessor in Objective-C bridges.
A copy of a class object is a shallow copy. Shallow copies are faster than deep copy, because of sharing the reference only. The created copy doesn't entirely create a new instance in memory, just address/reference is copied. As reference is shared, value change in a copy changes all the other.
var duplicateArray = originalArray is all you need. Note that there are pitfalls here that Swift's value semantics are working to protect you from—for example, since NSArray represents an immutable array, its copy method just returns a reference to itself, so the test above would yield unexpected results.
()
is both a type (and Void
is a type alias for the empty tuple type) and the only value of that type. So
let x = ()
defines x
as a constant property of type ()
(aka Void
) with the value ()
.
Equivalent statements would be
let x:() = ()
let x:Void = ()
but not
let x = Void // error: expected member name or constructor call after type name
because Void
is a type but not a value.
When defining function types with no parameters and/or no return value
there seems to be a consensus to use ()
for an empty parameter list,
and Void
for no return value. This can for example be seen in
UIKit completion handlers, or for execution blocks submitted to
GCD dispatch queues, e.g.
func sync(execute block: () -> Void)
Looking at the docs for the Swift grammar:
All tuple types contain two or more types, except for
Void
which is atype alias
for the empty tuple type,()
.
tuple-type → () | ( tuple-type-element , tuple-type-element-list )
tuple-type-element-list → tuple-type-element | tuple-type-element , tuple-type-element-list
tuple-type-element → element-name type-annotation | type
element-name → identifier
Therefore yes, it is a tuple of zero types / elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With