Reputation: 13551
This is an example of an Objective-C function I needed to translate to Swift
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo;
After much googling I finally found an answer where I could convert the (void *) to a valid Swift type. This type is apparently expressed as UnsafePointer<()>.
Do you know why (void*) translates to UnsafePointer<()> in Swift?
What is the syntax <()> called and what does it mean?
Upvotes: 2
Views: 915
Reputation: 535222
<...>
is a generic specifier. It resolves the generic placeholder of the generic type to whose name it is appended.
For example, Array is a generic, where the placeholder is its element type. So Array<String>
is the type of an array whose generic placeholder is specified as being a String - meaning, an Array whose elements are strings. You may say [String]
, but Array<String>
is equally valid.
Similarly, UnsafePointer is a generic, where the placeholder is the type of thing it points to. So UnsafePointer<Float>
is an unsafe pointer to a Float - which might be the first float in a C array of Floats. (That actually comes up in real-life Swift programming.)
()
is an empty tuple type, also known as Void
. For example, a function that returns no value returns ()
(or Void
).
So, putting it all together, UnsafePointer<()>
(or UnsafePointer<Void>
) is an unsafe pointer-to-void - which, by golly, is exactly what void*
is.
Upvotes: 4